.htaccess


Rewrite and redirect with php variables


I am simply trying to rewrite automatically this:
From: mysite.com/channel.php?id=BBC&name=British Broadcasting Company &date=today
To: mysite.com/channel-britishbroadcastingcompany-today.html
I've tried with:
RewriteRule ^channel-(.*)-(.*)\.html$ /channel.php?id=1&name=$2&date=$3 [R]
But nothing happens.
Hope this simplest one will help you out. This will redirect if
1. REQUEST_URI is /channel.php
2. QUERY_STRING matches this pattern id=something&name=something&date=something
Redirect this to /channel-%1-%2.html here
1. %1 will hold value of name parameter
2. %2 will hold value of date parameter
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_URI} ^/channel\.php$
RewriteCond %{QUERY_STRING} id=.*?&name=(.*?)&date=(.*)
RewriteRule .* /channel-%1-%2.html? [R=301]
See
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([a-z0-9-_.]+)/?$ index.php?id=$1 [NC,L]
RewriteRule ^([a-z0-9-_.]+)/([a-z0-9]+)/?$ index.php?id=$1&goto=$2 [NC,L]
What it's going to do is check the index.php and replace to some like, site/dir/index.php to site/dir/namehere than in index.php you can use explode() to separate the values of current url ang get the variables
If I understand the problem correctly, You currently have a file channel.php and what You want to achieve is get more "friendly" URLs for SEO and general aesthetics in the browser location bar but still have channel.php handle your requests.
If this is really the case then You need a two-way rewrite.
First, You need to take your original URL and redirect it to a new, pretty version.
Second, You need to rewrite this pretty URI internally and still feed it to channel.php behind the scenes.
RewriteEngine On
RewriteBase /
# This part rewrites channel.php?name=X&date=Y into channel-X-Y.html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_METHOD} =GET
RewriteCond %{QUERY_STRING} (.*\&)?name=([^&]+)\&date=([^&]+)(?:\&(.*))?
RewriteRule ^channel.php$ channel-%2-%3.html?%1%4 [R,L,NE]
# This part rewrites it back into channel.php but keeps the "friendly" URL visible
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^channel-(.*)-(.*).html$ channel.php?name=$1&date=$2 [L,QSA]
Note that the first rule-set limits the rewrite to method GET - otherwise You will lose any submitted POST data.
It also allows for any other query-string parameters to surround name and date (the rest of query-string parameters will pass-through to .html URI and then will be picked back up by channel.php)
Also note the ENV:REDIRECT_STATUS rule - this is crucial, without that part You'll be stuck in redirect loop.
I am assuming you are asking for rewrite although you are using redirect flag in your current rules, and also assuming BBC to be static in id variable then try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^channel-([^/]+)-([^/]+).html$ channel.php?id=BBC&name=$1&date=$2 [L]

Related Links

.htaccess Pretty URL with “?” insead of “&” param symbol?
Htaccess redirect from “www.domain.com/no” to “no.domain.com”
.htaccess URL Rewrite file extension and front controller using mod_rewrite
How to show only the domain.com not sub-folder
Handle the MEAN-stack application address without fragment '#'
Redirect to other domain except one subdirectory which needs to be redirected to a subdomin
Special rename with htaccess
how give permission to access /add-data.php on this server
Replace part of URL using .htaccess
Insert folder-segment into URL via .htaccess
force redirect to https via htaccess file
GZIP Compression is working for css/js but not for html
ReWriteEngine doing opposite of what is wanted
htaccess redirect 410 for URL that contains com_virtuemart
How can allow see but not download a file with .htaccess?
.htaccess if cookie does not exits, redirect

Categories

HOME
solr6
sendmail
entities
custom-attributes
slack
windows-forms-designer
mapkit
strongswan
bluetooth-lowenergy
gemfire
protege
openmeetings
widget
stormcrawler
mit-scratch
mupdf
unity3d-unet
cllocationmanager
tablesorter
prestashop-1.6
markup
visualvm
postmates
entity-relationship
django-channels
frama-c
nodemon
uppaal
white
webm
live
android-adapter
lsmeans
sharethis
polymer-serve
lektor
leanft
powerdesigner
gobject
smartassembly
fixture
maximo-anywhere
ektron
html.dropdownlistfor
falcor
sqlitestudio
allegro5
shared-ptr
dnspython
bisonc++
bubble-sort
erwin
code-signing-entitlements
outlook-2016
qt4.8
motorola
website-deployment
google-roads-api
highcharts-ng
jquery-ui-resizable
lg
zodb
cirqus
ssas-2008
foundation
angularjs-ng-options
gstat
xlet
github-linguist
ms-query
acronym
bpopup
android-bundle
voog
maven-site-plugin
uploadifive
jaggery-js
linux-capabilities
sqloledb
componentkit
fast-esp
angularjs-ng-show
textile
controltemplate
start-stop-daemon
microsoft-commerce-server
styledtext
harfbuzz
statamic
rounded-corners
stackmob
icommand
backbone-forms
converters
ora-01426
texinfo
cck
loose-coupling
visual-programming
mkstorekit

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile