Apache mod rewrite .htaccess - apache

I can get basic tests to work with mod rewrite ie:
RewriteEngine on
RewriteRule ^index.php$ test.php
But what I'm really trying to accomplish is changing my dynamic url ie:
detail.php?id=1
to something like
detail-id-18.htm
Now this is what I have in place (that I thought would have worked) but of course nothing is happening:
RewriteEngine on
RewriteRule detail-id-(.*)\.htm$ detail.php?id=$1

Try this
RewriteEngine On
RewriteBase /
RewriteRule ^detail-id-([0-9]+)\.htm$ detail.php?id=$1

Related

RewriteRule 301 redirect for whole directory and sub-directories

I really need your help with this one...
I'm simply trying to redirect EVERYTHING in a directory to another. It looks simple when I read about it, but in real life, it's not working... Here is my entire .htaccess file right now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Redirect all to HTTPS
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.org/$1 [R]
# End redirect
#301 REDIRECTS
Options +FollowSymLinks
RewriteRule ^mydir/(.*)$ /mydir-and-more/$1 [R=301,NC,L]
Fisrt, there is Wordpress stuff in didn't mess with.
Then, the code i copy/pasted from some site to redirect http to https. It works well. Note that i removed the "L" argument from the list to make sure my next rules will work.
After comes the part I'm strugling with.
So, it really is like that. My new directory starts with the same word then my old directory.
I copied this line from there: https://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
On the Apache web site (https://httpd.apache.org/docs/current/mod/mod_rewrite.html) it says that i should use a / between ^ and mydir. Tried it, didn't work.
I tried moving Options +FollowSymLinks at the top of the file. Nothing.
When i use something like this:
RedirectMatch 301 ^/mydir/ https://example.org/mydir-and-more/
This works. But only moves the exact /mydir/ address. It doesn't move the whole directory. Also, if I type in https://example.org/mydir without the last /, it won't work. If i add the / in the Redirect match, it doesn't work anymore because its the same word!
So, here I am, totally confused! Please, any expert advise on this one? Thanks!!
You need to move you rules before the wordpress defined ones.
In fact if you try to access you site the rewrite rules elaboration stops at
RewriteRule . /index.php [L]
That instruction means "manage all paths that are not already beign managed by upper rule and stop elaboration ([L] stands for last)".
You can safely place your rules above the wordpress ones, better in a ifmodule
<IfModule mod_rewrite.c>
RewriteRule ^/mydir/(.*)$ /mydir-and-more/$1 [R=301,NC,L]
</IfModule>
# BEGIN WordPress
Funniest thing is, I solved my problem by fooling around! I didn't really need the RewriteRule, all I needed to write instead of the RewriteRule was exactly this :
Redirect 301 /mydir https://example.org/mydir-and-more
I don't even need the Options +FollowSymLinks.

Mod_rewrite rewrite URL doesn't do anything

For some reason Apache doesn't rewrite my URL's and I can't figure out what's wrong.
I can confirm that mod_rewrite works because this works:
RewriteEngine on
RewriteRule ^oranges.html$ apples.html
And it shows the apples.html page.
I am trying to use this but it doesn't do anything!
Options -Multiviews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)[/]*$ index.php?page=$1
To look like this: http://example.com/search/
What could be wrong here? :o

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

modrewrite RewriteRule problem

I am using Apache 2.2 and mod_rewrite. I would like to take the following urls and map them to another url. The pattern is simple and like this:
http://domain/test/ex1.html -> http://domain/app/index.php/content/?name=ex1
I tried the following in an .htaccess file (place in /test/ directory in docroot):
RewriteEngine On
RewriteBase /app/
RewriteRule (.*)\.html index.php/content/?name=$1
and
RewriteEngine On
RewriteRule (.*)\.html /app/index.php/content/?name=$1
I wasn't sure if the backreference was correct so set to $0 and $2 but never seemed to help.
I also tried setting the RewriteLogLevel to 9.
There is a step where it is almost there:
rewrite 'ex1.html' -> 'index.php/content/?name=ex1'
The last line of the rewrite log is as follows:
[perdir /var/www/domain/htdocs/test/] internal redirect with /app/index.php/content/ [INTERNAL REDIRECT]
How can I get this to rewrite to /app/index.php/content/?name=ex1 ?
EDIT
so using this as an .htacces in the docroot file works for the RedirectMatch:
RewriteEngine On
RewriteBase /
RedirectMatch ^(.*)/(.*)\.html$ http://domain/app/index.php/content/?name=$2
#this doesnt work - adding to docroot allows for it to pick up but still using test/ex1 rather than just ex1
#RewriteRule ^(.*)\.html$ /app/index.php/content/?name=$1
Any help getting the bottom RewriteRule (that is currently commented out) to work would be appreciated.
thanks
In your example you mentioned domain/test/ so I'm going based of rewriting from /test/*.html to /app/index.php/content/?name=
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*)\.html$ /app/index.php/content/?name=$1
That should work.
Uhm, off the top of my head:
RewriteCond %{REQUEST_URI} ^/app/(.*)\.html$
RewriteRule ^/app/(.*)\.html$ /app/index.php/content/?name=$1
This assumes that the rewrite base is /

mod_rewrite and relative urls

I'm setting up some simple url rewriting rules using mod_rewrite and a .htacces file, but I've got some problems.
If I set up the .htacces this way:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html /index.php [L]
when I call from the browser this url:
http://localhost/~dave/mySite/index.html
I got a 404 error.
Using this .htacces instead
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule /index.html http://localhost/~dave/mySite/index.php [L]
everything works fine and I get the index.php page I'm expecting.
Am I forced to user absolute urls as the target of a rewrite? Is this a Apache configuration problem?
I'm using Max OS X 10.6.2 with its standard Apache installation.
The RewriteBase base directive is setting a base path; you don't need to provide absolute paths in the redirection rules.
The rule pattern is not plain text; it's a regular expression.
If you set a full URL as target you instruct the browser to redirect to that other location; that kind of redirection is not transparent to the user.
To sum up, try this instead:
RewriteRule ^index\.html$ index.php [L]
Try doing
RewriteRule ^(.*)/index.html $1/index.php [L]
That should sort it.