htaccess rewrite add trailing slash - php

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]

Related

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

Removing the last slash from query parameter using .htaccess

I want to remove the last slash character from query parameter's value using .htaccess.
Suppose I have the following URL:
www.example.com/?key_=/job_category/mobile/
Then from /job_category/mobile/ I want to remove the last slash.
After removing the last slash, it would redirect to www.example.com/snapshots/job_category/mobile.html, but with my current settings it redirects to www.example.com/snapshots/job_category/mobile/.html.
My current .htaccess file looks like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^key_=(.*)$
RewriteRule ^(.*)$ snapshots/%1.html [R=301,QSD]
You don't need to capture anything in RewriteRule, as all necessary information is already captured by RewriteCond. To remove the trailing slash use an expression like \/*$ (zero or more slashes at the end):
RewriteEngine on
RewriteCond %{QUERY_STRING} ^key_=(.*?)\/*$
RewriteRule ^.*$ /snapshot%1.html [R=301,QSD]

Could i have 2 links from the same page with htaccess?

So my htaccess lines look like this:
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ produse.php?categorie=$1&produs=$2
www.mysite.com/meniu/pizza/ works
www.mysite.com/meniu/pizza/Quatro_Formaggi/ doesn't work, it displays 404 not found.
Your URL has the underscore character
www.mysite.com/meniu/pizza/Quatro_Formaggi/
so just add the _ to the RewriteRule to match it
RewriteRule ^meniu/([a-zA-Z0-9]+)/$ produse.php?categorie=$1
RewriteRule ^meniu/([a-zA-Z0-9]+)/([a-zA-Z0-9_]+)/$ produse.php?categorie=$1&produs=$2
Your URL has a - (underscore character); and your rules don't; so you need to add the _ to the rule.
Also instead of using [a-zA-Z0-9] I would suggest using [a-z0-9] and the Not Case Sensitive flag ([NC]). So My suggested rules would be:
RewriteRule ^meniu/([a-z0-9]+)/$ produse.php?categorie=$1 [NC]
RewriteRule ^meniu/([a-z0-9]+)/([a-z0-9_]+)/$ produse.php?categorie=$1&produs=$2 [NC]
Also make sure you have a rule to add trailing slashes above this one or it will be annoying to any users hand entering the address to remember to have the trailing slash.

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

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.

.htaccess url rewrite not working witouth trailing slash?

I want to rewrite the URL of a page in my website. Its basicly really simple. My original URL looks like this
http://www.mypage.com/website/page.php?slug=my-page
I want it to look like this: http://wwww.mypage.com/website/my-page/
And that works. What doesent work is if you remove the trailing slash. This is my htaccess:
RewriteEngine on
RewriteRule ^(.*)/$ page.php?slug=$1 [L]
It seems as if i remove the slash the $_GET['slug'] becomes only page.php but with a trailing slash the variable says "my-page".
Is it possible to make it so the link works both without and with trailing slash?
Edit: Does it matter if i have the .htaccess and php file in a childfolder? So my real url is like this: http://www.mypage.com/website/page.php?slug=something
I've now edited the post with how it really is.
Try adding this:
RewriteRule ^website/([a-z0-9]+)/?$ website/page.php?slug=$1 [NC,L,QSA]
Amend the regex as needed depending on the type of URLs you want to accept.
Look up how htaccess works with regard to things like querystrings and so on:
http://httpd.apache.org/docs/1.3/howto/htaccess.html
RewriteEngine on
RewriteRule ^(.*)/?$ page.php?slug=$1 [L]
The question mark makes the trailing slash optional. I would also suggest to have only one kind of URLs. Like redirect URLs without the trailing salsh, to the one with.
I can only think of using this line before RewriteRule ^(.*)/$ page.php?slug=$1 [L]
RewriteRule ^(.*)$ page.php?slug=$1 [L]
Basically, in your original code, you declared a trailing slash, so it requires a trailing slash

Categories