I have a series of price lists from various manufacturers (spat out from a database). They are limited to 100 rows per page, ie:
www.domain.com/products/price-lists/company1.php?page=2
www.domain.com/products/price-lists/company2.php?page=10
which i want to rewrite to:
www.domain.com/products/price-lists/company1/page/2
www.domain.com/products/price-lists/company2/page/10
I have:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^products/price-lists/company1/page/([0-9]+)/?$ products/price-lists/company1.php?page=$1 [NC,L]
RewriteRule ^products/price-lists/company2/page/([0-9]+)/?$ products/price-lists/company2.php?page=$1 [NC,L]
First of all, this throws a 404 if i try to go to /products/price-lists/company1/page/2 (it works if i remove /page/) and second, i was wondering if i could combine them into one rule anyway:
(company1|company2[0-9]+)/?
Thanks for any help (this is my first mod_rewrite attempt!)
Try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^products/price-lists/([a-z0-9_\-]+)(/page)?/?$ products/price-lists/$1.php?page=1 [NC,L]
RewriteRule ^products/price-lists/([a-z0-9_\-]+)/page/([0-9]+)/?$ products/price-lists/$1.php?page=$2 [NC,L]
The first rule will check if a page is there or not without a number and go to page 1. The ([a-z0-9_\-]+) will be use to put the company, so if you put:
http://www.domain.com/products/price-lists/stackoverflow/page/2
the /products/price-lists/stackoverflow.php will open using $_GET['page'] = 2
The problem is that the part of the url is the same as the filename of the php file (without the .php). Use the following to correct.
options -multiviews
Related
I have updated country code in the base path and I am attempting the redirect users that end up going to:
www.example.com/uk/etc1/etc2/etc..
www.example.com/gb/etc1/etc2/etc..
But also sometimes also www.example.com/uk#etc
I thought this would be fairly trivial in mod_rewrite I cant get anything to work.
The closest I feel I have come is the following rule, but nothing happens when I land on uk.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* index.php [L] #this is active already on the page
RewriteRule ^/uk(.*) /gb(.*) [R]
</IfModule>
Edit:
This seems closer but I am getting stuck in an endless loop:
RewriteRule ^uk(.*) /gb$1 [R]
Edit 2 / (3) Answer:
This seems to redirect correctly(is correct):
RewriteRule ^uk(.*)$ /gb$1 [R=302,L]
RewriteRule .* index.php [L]
But the formatting is broken on the page, looks like some loop problem.
But the formatting is incorrect, If I swap the two RewriteRules the formatting is correct but the uk => gb conversion doesn't seem to get called at all.
Answer
Be careful of Rewriteconditions, I failed to notice the significance of them and I removed them for the original index.php rewrite which meant it could not locate any media.
I failed to take notice of the rewrite conditions which applied for the original index.php
This should do the trick:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^/uk(.*) /gb$1 [R=301,L]
I'm working on a PERL site which uses the medoc ecommerce system, and I've found that is spits out really ugly URL's which have no meaning to users or search engines:
https://www.example.com/cgi-bin/live/ecommerce.pl?site=test.com&dept_id=01&sub_dept_id=01&menu=currency&zone=test
I want to rewrite this so it's something simple like https://www.example.com/travel-money/home-delivery
Any ideas how I would go about this?
There are perhaps 50 different cgi-bin URL's each with multiple parameters, and I'd like to rewrite all of them individually.
I came up with two different rewrite rules, but neither worked. I couldn't find any examples with multiple parameters:
Option 1:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^site=test.com&dept_id=01&sub_dept_id=01&menu=currency&zone=test$
RewriteRule ^/cgi-bin/live/ecommerce.pl https://www.example.com/travel-money/home-delivery [L,R=301]
Option 2:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)test.com&dept_id=01&sub_dept_id=01&menu=currency&zone=ice$
RewriteRule ^cgi-bin/live/ecommerce.pl$ travel-money/home-delivery? [R=301,L]
Thanks,
i have such .htaccess:
RewriteEngine On
# http://site.com/ru,ua,lt/anything/
RewriteRule ^(ru|ua|lt)/([^/]+)/$ index.php?lang=$1&article_id=$2
# http://site.com/print/ru,ua,lt/anything/
RewriteRule ^print/(ru|ua|lt)/([^/]+)/$ print.php?lang=$1&article_id=$2
First example(# http://site.com/ru,ua,lt/anything/) works, second not work. Please help solve situation.
First of all change your code to this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# http://site.com/ru,ua,lt/anything/
RewriteRule ^(ru|ua|lt)/([^/]+)/?$ index.php?lang=$1&article_id=$2 [L,NC,QSA]
# http://site.com/print/ru,ua,lt/anything/
RewriteRule ^print/(ru|ua|lt)/([^/]+)/?$ print.php?lang=$1&article_id=$2 [L,NC,QSA]
Then try out your URIs and report back if it still doesn't work.
What does not work? Is it only external files (JS, CSS, Images) that do not work?
This could be the case, for your second URL has an additional sub directory. You should then use absolute URL paths to link to external files.
I want to rewrite urls like index.php?c=4 & index.php?g=23 into website.com/games/categoryname/id/
and the same thing for the game page website.com/play/gamename/id/
my htaccess file looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3
RewriteRule ^([a-zA-Z0-9\-_%]+)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$3
The problem is that only the first rewrite rule is working, if I comment it, then the second will work too, but never both :(. I'm testing this on MAMP
Can you please help me?
They cannot work both as they have the same condition - you have set two different actions with the same criteria and only the first one is executed.
Ah, I understood what you are trying to achieve:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(games)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$3 [L]
RewriteRule ^(play)/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$3 [L]
You have to do something like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^play/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?c=$2
RewriteRule ^games/([a-zA-Z0-9\-_%]+)/([0-9]+)/$ index.php?g=$2
I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]