Apache RewriteRule not working without Page # specified - apache

I have a rewrite rule set up in my .htaccess file:
RewriteRule ^Crocodile-Style/([0-9]+)/?$ products/display.php?folder=crocodile-style&page=$1 [L,NC]
http://test.bradp.com/drupal/Crocodile-Style/1 works OK.
http://test.bradp.com/drupal/Crocodile-Style/ DOES NOT WORK.
Apache throws a 404. The PHP logic defaults to page 1 without a page specified, so I know the script is fine.
What can I do?
Thanks
Nick

It's probably easiest to implement this with two rules:
RewriteRule ^Crocodile-Style/?$ products/display.php?folder=crocodile-style [L,NC]
RewriteRule ^Crocodile-Style/([0-9]+)/?$ products/display.php?folder=crocodile-style&page=$1 [L,NC]

Related

Weird mod_rewrite behavior

I'm trying to redirect all specific file extensions to the CDN I have setup. Here's what I'm working with:
RewriteRule ^(.*\.(jpe?g|gif|bmp|png|css|js|txt|wmv|mp4|flv|avi|mov|wmv))$ http://cdn-w.domain.com/$1 [L,NC]
For some reason it's only redirecting to (for example http://www.domain.com/images/image.png)
http://cdn-w.domain.com/image.png
rather than
http://cdn-w.domain.com/images/image.png
Why is that?
Try this rule:
RewriteRule \.(jpe?g|gif|bmp|png|css|js|txt|wmv|mp4|flv|avi|mov|wmv)$ http://cdn-w.domain.com%{REQUEST_URI} [R=301,L,NC]

Rewrite URLs Using .htaccess

Referring to the article here I created my .htaccess file with the following lines
RewriteEngine on
RewriteRule ^/product/([0-9]+)/(.*)$ /index.php?route=product&product=$1 [L]
but this doesn't work and I get a 404 error.
I want URLs like
http://localhost/product/12/some-random-text.html
to be redirected to
http://localhost/index.php?route=product&product=12
I am using GoDaddy's Linux hosting
Try changing your RewriteRule to:
RewriteRule ^product/([0-9]+)/(.*)$ /index.php?route=product&product=$1 [QSA,L]

URL Rewriting using .htaccess

To change the URL /mobiles.php?id=5 to /mobiles/5
The content of .htaccess file is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$
But still it is showing /mobiles.php?id=5 in the address bar. Please help. Is there anything else needs to be added in the .htaccess file?
Note:
mod_rewrite module is enabled
I have restarted Apache server after making changes to the .htaccess
file
.htaccess file is in htdocs folder of Apache.
I am using Windows + PHP + Apache + MySQL
This works for me:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]
If you see this line:
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
I have added rew variable in the query string to prevent Apache to fall in an infinite loop
When Apache execute this line:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
Is to make sure that url has not been rewritten for Apache
If your only concern is that the old url stays in the address bar, and you want this not to happen, try adding an [R] at the end.
RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]
Did you actually see the correct page?
By the way, the rewrite rules generally go the other way. I would be expecting to see something like:
RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1
Is your concern one of making sure a URL with query parameters does not show up in the address bar?
If I understand correctly, you want
Internally redirect /mobiles/5 to /mobiles.php?id=5
Also redirect the browser TO /mobiles/5 if a user navigates to /mobiles.php?id=5
For this you need 2 rules one to internally rewrite the URL for 1st case and 2nd for browser redirection.
You can do it like this:
RewriteEngine on
# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]
# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]
You are doing the opposite, should be:
RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1

RewriteRule for URLs that do not end with dot html/php

I would like to redirect all:
to
I have started to
RewriteRule !\.(html|php)$ /get.php?file=$1 [PT]
But it does not work.
I've tested this regexp in PHP it should work in mod_rewrite but I have not tested it and seems to work in mod_rewrite too:
RewriteRule ^(.+)(?<!\.php)(?<!\.html)$ /get.php?file=$1
The rule will rewrite all URLs except those that end with .php or .html.
The most basic way to handle that is:
RewriteRule ^(.*)$ get.php?file=$1 [QSA,L]
Which will redirect everything, setting file to the value of the path.

RewriteRule redirect

Okay I have this RewriteRule which is supposed to redirect any request for the file base.css to {folder of .htacces file}/include/style/base.css, but is just keeps redirecting in an infinite loop, I thought the L parameter would make sure that wouldn't happen.
RewriteRule (.*)/base.css$ include/style/base.css [L,NC,R=301]
Also it redirects to http://localhost/C:/somemaps/include/style/base.css which it isn't really supposed to do either.
Can anyone tell me how to fix this?
Also I would like to have the RewriteRule so it would redirect any file.css to {folder of .htacces file}/include/style/file.css
BTW the .htacces file is in the root of the website (which is not the root of the server!)
You have Redirect and Rewrite confused. A redirect is a HTTP status code that tells the browser to go to another URL. You actually just want to Rewrite the location to another file location. Try
RewriteRule (.*)/(.*).css$ /include/style/$2.css [L,NC]
If this doesn't work try adding the following right after the RewriteEngine On
RewriteBase /my-virtual-folder-path-where-htaccess-is-stored
Also I would like to have the RewriteRule so it would redirect any file.css to {folder of .htacces file}/include/style/file.css
Try this:
RewriteRule ([^/]+).css$ /include/style/$1.css [L,NC]
This R=301 makes a new request. Therefor it evaluates the RewriteRule again.
Try to exclude this path/directory with a rewrite condition (RewriteCond).