Using Mod Rewrite to rewrite a url? - php

I have the following format:
http://www.mydomain.com/view/product/ID/CAT/TITLE
ID and CAT are both numeric. So, some real examples:
http://www.mydomain.com/view/product/1/2/ipod
http://www.mydomain.com/view/product/3/4/40-inch-tv
etc
I want to know if I should try to use mod rewrite, and how, or if I should rather fix the PHP to handle the paths correctly. I want to map the above to something like:
http://www.mydomain.com/CATEGORY/SUB_CATEGORY/TITLE
Any ideas?

RewriteEngine on
RewriteBase /
RewriteRule ^([A-Za-z0-9]+)$ /$1/ [R]
RewriteRule ^([A-Za-z0-9]+)$ index.php?cat=$1
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/$3/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2&title=$3
in php you can get the variables as you would normally do, also you will need a rule for all variables entered and/or only category or sub-category or you will end up with a 404 when someone tries to access those pages

I would say that your question is not totally clear to me. Going by your examples what do you want your target URL to become from http://www.mydomain.com/view/product/1/2/ipod.
Will it be: http://www.mydomain.com/2/1/ipod ?
If yes then enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^view/product/([0-9]+)/([0-9]+)/(.*)$ $2/$1/$3 [L,R,NC]

Related

.htaccess with multiple rules

I want to create an htaccess file with the following rules:
I want to redirect:
http://computingessentials.tk/episodes/6
To:
http://computingessentials.tk/episodes.php?id=6
Same applies with the remaining episode.php ids
Next, I want to redirect:
http://computingessentials.tk/episode.php?id=56
To:
http://computingessentials.tk/episode/56
Same applies with the remaining episode.php ids
And last but not the least, I want to remove all the .php extensions of the files.
Since I am farlynew to htaccess i don't really know how to get all of these in an htaccess.
Help would be very much appreciated.
Make sure your mod rewrite of apache is on and put these rules in .htaccess
RewriteEngine on
RewriteRule ^episodes/([0-9]+)$ episodes.php?id=$1 [L]
You can use these 2 rules in your root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episodes\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /episodes/%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^episodes/([^/.]+)/?$ /episodes.php?id=$1 [L,QSA,NC]

How to use htaccess code for URL rewrite in PHP?

I have a link like below:
In /country/search.php
<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">
When I use the below .htaccess code, it does nothing:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L]
It does in the tool:
http://example.com/0/0/4/122
... but when I click that link it shows old URL again.
What is the problem here?
I'm generating that .htaccess code using this tool: http://www.generateit.net/mod-rewrite/
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]
# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]
I think you misunderstood what the rewrite rules are trying to achieve in this context.
F.e. this rewrite rule
RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2
Will ensure that a request to http://example.com/mymethod/myarg will be rewritten to
http://example.com/app.php?method=mymethod&arg=myarg
When you generate links from within your application you should know how you contstructed the rewrite rules and you have to build the link accordingly.
Original URL:
href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>
Ideal URL:
/India/andhra%20Pradesh
And .htaccess code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

Rewrite rules not working htaccess

I have the following htaccess rewrite rules
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
RewriteRule ^shows-watch/(.*)/season-(.*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/(.*)/season-(.*)/episode-(.*).html?$ show.php?name=$1&season=$2&episode=$3
Now the thing is, the first rewrite rule works just fine
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
It's just when I try using the others, only name get variable is being passed and not
$_GET['season']
or
$_GET['episode']
i know it's most likely something simple I'm missing or have done, but I just can't seem to get it working.
Give this a try:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^shows-watch/([^/]+)/season-([^/]+)/episode-([^/]+).html?$ show.php?name=$1&season=$2&episode=$3 [QSA,NC,L]
RewriteRule ^shows-watch/([^/]+)/season-([^/]+).html?$ show.php?name=$1&season=$2 [QSA,NC,L]
RewriteRule ^shows-watch/([^.]+)\.html?$ show.php?name=$1 [QSA,NC,L]
The order is very important so they don't overlap you also need the L flag to stop when needed.
This assumes your .htaccess is on the root folder of your domain along with the show.php file and that you are accessing it like this:
domain.com/shows-watch/Show Name.html
domain.com/shows-watch/Show Name/season-1.html
domain.com/shows-watch/Show Name/season-1/episode-10.html
The first line gets all the links because it matches all the links.
Try reverse the order:
RewriteRule ^shows-watch/(.*)/season-(.*)/episode-(.*).html?$ show.php?name=$1&season=$2&episode=$3
RewriteRule ^shows-watch/(.*)/season-(.*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/(.*).html?$ show.php?name=$1
Or you can exclude slashes like this:
RewriteRule ^shows-watch/([^/]*).html?$ show.php?name=$1
RewriteRule ^shows-watch/([^/]*)/season-([^/]*).html?$ show.php?name=$1&season=$2
RewriteRule ^shows-watch/([^/]*)/season-([^/]*)/episode-([^/]*).html?$ show.php?name=$1&season=$2&episode=$3

Hide a part of a URL with mod_rewrite but keep the argument

I would like to write a rule where the members/show/ string will be hidden in the URL.
The original URL: localhost:8080/sandbox/member/show/helloworld, where helloworld is the name of the account. I'd like it to be localhost:8080/sandbox/m/helloworld. Basically, the content delivered will be from the full url, but I'd like it to be hidden for any user.
RewriteRule ^.*$ member/show/$0 [L,QSA] doesn't seem to work, it throws a 500 Internal Server Error. I work with the CodeIgniter Framework, and the following rewrite rule is already present: RewriteRule .* index.php/$0 [PT,L].
I tried several RewriteRule options but without success. I'd be very glad if anyone could shed some light related to my question.
Best regards.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(sandbox)/m/([^/]+)/?$ /$1/member/show/$2 [L,NC]

.htaccess file not redirecting - guidance wanted

I have this url:
details
the url becomes in addressbar:
http://web228.sydney.webhoster.ag/soputnik/drive_to_london/?procid=12
but I want to process the logic in details.php.
How can I process the data in details.php in background and keep the first url in the address?
I tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^drive_to_(.*)$ http://web228.sydney.webhoster.ag/soputnik/details.php [R=301,L]
but it is not working, error is: NOT FOUND
please help
Substitute your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^test/drive_to_(.*?)/?$ /test/details.php [L,NC]
If you don't want the URL to change, then it's not a redirect, just a rewrite.
Change:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php [R=301,L]
To something like:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php?city=$1 [L]
dont forget to pass any url params to details.php.
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$1 [R=301,L]
it seems relevant that you want to pass the city name as well, otherwise that context gets lost in the rewrite. If that is another url param you could pass it through to details.php as such
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$2&city=$1 [R=301,L]

Categories