.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

Codeigniter index page not visible
how to put fontawesome inside cache using htaccess
Working with both old and new URL using htaccess
How to redirect root domain to subfolder (with https) and rest of addon domains to subfolders (without https)
.htaccess access parent folder
Redirect http to https only in one page and the others pages https to http
how to redirect all subdomains globally to parent domain
Url redirection issue with redirect 301
How to remove https
301 Redirect - preg match rule in .htaccess?
Replace URL query string with slash for a friendly URL
CodeIgniter 404 Routing - 404 broken links and inability for google to crawl site BUT everything looks ok
If we set browser caching for a JS or CSS file via .htaccess what happens if we need to push a fix ASAP?
Using .htaccess how can I create a redirect based on if a string exists in the URL?
dns propogation issue with cloudflare subdomain
htaccess redirect not working for long url

Categories

HOME
menu
java-ee
ruby-on-rails-4
reverse-geocoding
veins
refactoring
form-submit
powerbuilder
webserver
timezone
access-token
postgis
android-toast
snapshot
dijkstra
navbar
sql-azure
codecvt
multiline
qtspim
entity-relationship
ractivejs
angular-openlayers
na
spacing
spring-4
modeling
asp.net-mvc-routing
appery.io
yamldotnet
nestedscrollview
pymodbus3
tridion
homescreen
actor
keyword-search
itunes-store
cartography
javac
jsdoc3
autocompleteextender
powerview
erwin
cpio
nslog
z-order
sharepoint-userprofile
raptor
jodconverter
kendo-datepicker
classname
graphic-effects
polymer-designer-tool
gridster
velocity.js
mpld3
7digital
meter
xlet
eazfuscator
er-diagrams
lastaccesstime
rich-snippets
wpftoolkit
virtual-address-space
desktop-shortcut
ember.js-view
teamsite
deface
restriction
httpwatch
countries
texmacs
harfbuzz
craftyjs
jbossws
perl-critic
amber-smalltalk
ios6.1
array-formulas
set-theory
icarousel
generic-list
osx-gatekeeper
scalaquery
hibernate-annotations
flotr
eda
dft
context-sensitive-help

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App