mod rewrite - why the trailing slash is not optional here? - php

I got some rules in my .htaccess I use ?/ to make the trailing slash optional, but in the last of my rueles it doesn't work. The page loads only if the slash is present, otherwise the server gives a 404 error
The last two rules are:
RewriteRule ^page?/$ page.php
RewriteRule ^otherpage/([a-zA-Z0-9_\-]+)?/$ otherpage.php?slug=$1 [L]
I can't understand why, what should I do to make the trailing slash in the last rule optional?
Thanks for the help.

Related

htaccess rewrite add trailing slash

I have following url :
http://localhost/PROJECTNAME?gallery_detail.php?id=1
Which i am rewriting to this : http://localhost/PROJECTNAME/username/id
I have used following code for that:
RewriteRule /(\w+)$ gallery_detail.php?id=$1 [L]
RewriteRule /$ gallery_detail.php?id=$1 [L]
And it's working fine but when i am removing the id and slash it redirects me to the 404 page. how can i forcefully add trail slash at the end of the url or is there any other way to do that.
Please help!!!
It's because you are using a leading slash in RewriteRule's pattern (See your first rule) . htaccess doesnot accept leading slash in its pattern,you need to remove this. You can use the following rule with an optional trailing slash :
RewriteRule ^(\w+)/?$ /gallery_detail.php?id=$1 [L]

rewrite wordpress url for specific page to remove trailing slash and add php extention

I want to remove trailing slash for a specific page in Wordpress and do some another changes.for example I have this page :
http://www.domain.com/page/
I want to change the url to this :
http://www.domain.com/page.php
you know because I need send some get request and its so ugly with that trailing slash so any one can help?
thank you.
You would then need to use a rule like the following in the .htaccess file in your www-root:
RewriteCond %{REQUEST_URI} !(page)/$
RewriteRule ^(page)$ http://example.com/page.php/ [L,R=301]
You can use the following Redirect
RedirectMatch ^/page/?$ /page.php
/? in the pattern above means the trailing slash is optional. This will redirect both /page or /page/ to /page.php

htaccess RewriteRule doesn't work properly if no trailing slash

I am trying to redirect one subdirectory to another using RewriteRule in the main directory's .htaccess file.
For example: http://website.com/subdir should rewrite to http://website.com/another_dir/destination_dir but the user should still see http://website.com/subdir in the address bar.
This works perfectly if the user ends the URL with a trailing slash. Example: http://website.com/subdir/ (Generates a 200
However, if the slash is omitted, a 301 redirect is generated and we see the undesired destination directory. For example, http://website/subdir redirects the user to http://website/another_dir/destination_dir
Here are the pertinent parts of .htaccess:
# URL Rewriting:
RewriteEngine On
RewriteBase /
...
# Redirect subdirectories:
RewriteRule ^subdir(.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]
Any assistance is appreciated!
You can adjust the regex in your rewrite rule to optionally match a slash.
RewriteRule ^subdir(/?.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]
Note the /? just after subdir. This says that there may or may not be a slash just after your subdir, so the regex will match either way.
A quick fix would be
DirectorySlash OFF in this htaccess, and DirectorySlash ON a .htaccess in another_dir/

How do I redirect specific URLs to specific pages using mod_rewrite?

I'd like to redirect localhost/website/setup to localhost/website/setup.php.
This is the mod_rewrite rule currently used:
RewriteRule ^setup/$ setup.php [L]
This works for redirecting localhost/website/setup/ to localhost/website/setup.php. However, as soon as the trailing slash is removed, it no longer works (it redirects me to localhost/setup/), even if I do not add the trailing slash in the rewrite rule. What am I doing wrong?
This part ^setup/$ is regular expressions so you can just do this
RewriteRule ^setup/*$ setup.php [L]

How do I get rid of the extra slash at the end of the url

I have this link and as you can see there is an extra / at the end. Basically i have a restaurant_pos folder with an index.php file in it but apache is adding the extra slash. I thought there was a way to do this in htaccess with directory directives off or something like that...not sure the name...any ideas
This should do the job for you (redirect if UR has trailing slash):
DirectorySlash Off
RewriteEngine On
RewriteBase /
# get rid of trailing slash
RewriteRule ^(.*)/$ $1 [R=301,L]
But you may have that extra slash added by some other rule in your .htaccess. If above will not help and you have some rewrite rules already in place -- please post them in your question.

Categories