.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

getting an error "Invalid command 'SSLProxyCheckPeerCN'
Remove amp from url .htaccess
.htaccess rewrite subdirectory to parent directory
How to allow some specifics URLs and rest of others would be redirect to maintenance page
Create .htaccess Wildcard with Subdomains
Redirecting web content but not the name of web page address
Htaccess rewrite rule code
What's the code should behind this situation?
no luck in rewriting url with query string
How can I remove this directory/folder from url with htaccess rewrite?
RewriteRule htaccess if a file exists
mod_deflate compression causing swf files to not work correctly
How to remove extra character from http request?
HTACCESS short url and modify my url
htacces rewrite rule from normal url to php parameter
Remove directory in htaccess with mod_rewrite

Categories

HOME
linux-kernel
objective-c
neural-network
google-maps-api-3
statistics
asp-classic
sharppcap
amazon-redshift
etcd
wlan
mips32
google-vr
relation
ninja
corba
video.js
filenet-p8
batch-normalization
bit-manipulation
derivative
build-automation
konvajs
bootstrap-select
cpu-registers
flip-flop
jsx
preview
scons
nodemon
jqassistant
puma
x509
appery.io
gperftools
nestedscrollview
state
javapoet
flops
lektor
spamassassin
gunicorn
thinktecture-ident-model
nn
transitions
android-runonuithread
redhat-bpm
nerdtree
data-compression
enzyme
xilinx-edk
jes
imei
trace
reduce
exceljs
javassist
django-compressor
unicode-string
android-gson
cscope
stdio
visual-c++-2013
ms-project-server-2013
raptor
flowgear
display-bugs
mapfragment
onbeforeunload
antivirus-integration
r1soft
integer-overflow
ooyala
objdump
merit-gem
acronym
utilities
qmediaplayer
gamma-function
puphpet
voog
ironscheme
centura
wpftoolkit
iec61131-3
pegjs
kiwi
cxf-client
datacontext
start-stop-daemon
firstdata
nsurlprotocol
usermode
socketrocket
apiaxle
styledtext
django-1.2
inter-process-communicat
c++builder-6
mat-file
django-staticfiles
mathtype
icarousel
django-sessions
ejbql
visualtreehelper
windows-networking
layar
tunneling
set-include-path
fql.multiquery
mahjong
winqual
clean-language
incremental-linking
habari
o3d

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