.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

Is there a correct location to place favicon / icon files in my web root?
Rewrite Long URL using htaccess
New Url Problum In No input file specified Solution Codeigniter
URL re-writing using in .htaccess and query strings
.htaccess redirecting twice
htaccess url rewrite index.php?page=pagename
Url rewrite with parameter using .htaccess
RewriteMap not working to prevent hotlinking
Non-existent pages return 301 and 302
htaccess - insert additional path
Minimize your URLs in Codeigniter
How can i 301 redirect a subfolder and everything after it?
Expire headers htacces for phpThumb
I want to add rewriteurls in my existing .htacess file.. But when i add new its not working
Adding expire headers in htaccess
Google webmaster URL crawl error - 301 moved permanently

Categories

HOME
menu
sails.js
magento-1.9
hardware
drools
kotlin
node-red
chart.js
imageview
chromium
mutex
facebook-live-api
distributed-database
spf
worksheet-function
angularjs-ng-transclude
dijkstra
row-number
affinity
video-processing
tablesorter
accelerometer
4d-database
postmates
chinese-locale
provider
servicestack.redis
fixtures
thinktecture-ident-model
scada
youtube-analytics
smtpclient
webex
nerdtree
xilinx-edk
oracle-sql-data-modeler
fixture
ansible-vault
singleton-type
khan-academy
paypal-adaptive-payments
google-truth
autocorrect
modelio
xunit.net
mypy
object-oriented-analysis
knockout-validation
bisonc++
autocompleteextender
nuodb
icingaweb2
android-gson
angularjs-service
google-url-shortener
fixed
embedded-javascript
playstation
android-runtime
knox-gateway
nested-function
mechanize
devpartner
nsthread
nibble
reducers
oql
stl-format
mime-mail
scrapyd
ios9.3
velocity.js
pysqlite
busy-waiting
offsetheight
sin
sql-agent-job
aloha-editor
ekeventstore
soundex
.a
xcode-organizer
desktop-shortcut
subsonic2.2
syntactic-sugar
option
angularjs-ng-show
basic-msi
sine-wave
.class-file
server-side-scripting
circusd
incompatibletypeerror
iphone-4
scalaquery
nscoder
live-cd
flashlite

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile