.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

Wrong domain mapping with redirecting to another domain
.htaccess redirect subdomain to another subdomain directory
.htaccess code redirect and rewrite
htaccess multilanguage in codoforum script
301 redirect with url parameters
mod_rewrite for SEO friendly URL using htaccess
Htaccess query string to another
htaccess rule to block referring URLs and URLs containing “administrator”
htaccess file doesn't work in subdirectory
mod_rewrite redirect everything except special URLs and files
Will google bot follow multiple redirections (by .htaccess)
Parameter exception in htaccess rewrite
Hide the path in website URL using PHP
redirect to mobile site using htaccess
.htaccess: rewrite to new URL structure
URL Rewriting on multiples folders

Categories

HOME
web
firmware
function
accounting
asp-classic
cakephp-3.0
linux-device-driver
speech-recognition
graph-algorithm
ais
amazon-cloudsearch
protege
codeception
bigdata
lokijs
pthreads
markup
odbc
.net-4.5
image-comparison
cpu-registers
gun
boost-python
amazon-lightsail
navigation-drawer
akka-stream
data-migration
postgresql-8.3
javabeans
lsmeans
nestedscrollview
cassandra-3.0
lektor
spamassassin
leanft
powerdesigner
apache-hive
scada
data-storage
devexpress-wpf
uievent
configuration-files
jes
imei
lighttable
xc8
dwt
api-manager
exceljs
wistia
smo
appharbor
invoke
mypy
mongodb-scala
date-formatting
android-runtime
leshan
helium
htsql
swig-template
nested-attributes
servant
nscoding
arrangeoverride
polymer-designer-tool
r1soft
opcode
singularitygs
xcode-bots
adobe-dps
ssas-2008
angularjs-ng-options
scrollviewer
google-text-to-speech
xcode-organizer
curator
chap-links-library
inference
problem-steps-recorder
nodejitsu
jsonexception
static-array
mysql-error-1052
phonetics
springmockito
generic-list
cloning
windows-networking
validationsummary
trx
resource-cleanup
isaserver
flashlite

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile