.htaccess url rewrite not working witouth trailing slash? - php

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

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]

Trouble mapping multiple subfolders to destinations in htaccess

We've switched servers and for whatever reason, our htaccess file isn't behaving the same way as it was on the other. I'm going to be the first to admit that I'm not an htaccess superuser, and I have no doubt the answer's probably looking me in the face, but literally an entire Saturday of searches hasn't fixed this. I have this filesystem:
When the domain is /category/subcategory/product/
It should rewrite to /category/product-details.php?p=product&s=subcategory
When the domain is /category/subcategory/
It should rewrite to /category-product-list.php?slug=subcategory
This seems simple enough, here's the same code we had been using for years. Note, we've commented out the first two RewriteRules regarding slashes and there was no change in behavior.
RewriteEngine On
RewriteBase /
# if folder does not end with a slash redirect to with slash
RewriteRule ^([-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
#if it does not end with a slash e.g. rock-jewelry/some-piece, add the slash
RewriteRule ^([-a-zA-Z0-9]+/[-a-zA-Z0-9]+)$ /$1/ [L,NC,R=301]
RewriteRule ^category/([^/]+)/([^/]+)/?$ category/product-details.php?p=$2s=$1 [L,QSA]
RewriteRule ^category/([^/]+)/?$ category-product-list.php?slug=$1 [L,QSA]
When the domain is /category/subcategory/product/
It rewrites to /category-product-list.php?slug=product-details.php&p=product&s=subcategory
When the domain is /category/subcategory/
It correctly rewrites to /category-product-list.php?slug=subcategory
/category/ is an actual folder, and the only thing in it is product-details.php, there is no index.php file, the htaccess is supposed to rewrite to category-product-list if they're trying to access the index.
If we remove the category-product-list.php rule, the product-details.php rule DOES work. But isn't the L directive supposed to stop at the first rule? Why is the second still running? And how can I write a better way to accomplish this goal? Thank you very much, I'm pretty beat down on this problem at this point.
I have the same problem as you to understand the why of this loop... But it's like that.
You can add that before the last RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
Or if you don't use dot in subcategory, you can use final RewriteRule:
RewriteRule ^category/([^./]+)/?$ category-product-list.php?slug=$1 [L,QSA]

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.

rewrite a folder so all files display under new rewrite rule

Is there a way that I can rewrite a folder so that all the files under that folder follow the same rule? For example:
if i have a folder with say 5 php files (a.php, b.php, c.php, d.php, index.php) in it and i use the following rule:
RewriteRule ^products/storage/?$ /content/products/storage/index.php [QSA,L]
is there a way that I can get all the files to show to be accessed like: site.com/products/apples/a.php, site.com/products/apples/b.php, etc. without having to write a rule for each one?
I tried the following but it didnt work.
RewriteRule ^products/storage/?$ /content/products/storage/ [QSA,L]
I also need it to NOT overwrite my other rules such as:
RewriteRule ^products/storage/?$ /content/products/storage/product-name1/ [QSA,L]
RewriteRule ^products/storage/?$ /content/products/storage/product-name2/ [QSA,L]
any ideas?
Your problem is the trailing $ on the end of the regex. This will only allow a match if the full URI matches products/storage (with optional trailing slash) exactly. Instead, try the following and note the absence of the trailing $ character:
RewriteRule ^products/storage/? /content/products/storage/ [QSA,L]
This will match anything that starts with products/storage (with optional trailing slash). Alternatively, if you wanted to capture and re-use everything in the URI that followed products/storage/ you could try:
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
UPDATE
Should you need to preserve other RewriteRules as your updated question suggests, you should look to add a RewriteCond condition like so:
RewriteCond !^products/storage/?$
RewriteRule ^products/storage(/?.+)?$ /content/products/storage$1 [QSA,L]
The RewriteCond tells the RewriteRule to only process if the condition is not met.

mod_rewrite multiple directories to a php file

RewriteEngine on
RewriteRule ^/(dir1|dir2|dir3)/(.*)$ /targetfile.php [R,L]
http://www.somesite.com/dir1 -> http://www.somesite.com/targetfile.php
http://www.somesite.com/dir2 -> http://www.somesite.com/targetfile.php
http://www.somesite.com/dir3 -> http://www.somesite.com/targetfile.php
From what I've seen online, this should work. Unfortunately, it wont. Any insight?
If you want to use this in a .htaccess file, remove the leading slash from the pattern. And to match only full path segments, you have to alter the expression a little bit.
So try this:
RewriteEngine on
RewriteRule ^(dir1|dir2|dir3)(/|$) targetfile.php [R,L]
I don't believe the forward slashes are necessary, unless you want to restrict it to requiring the trailing slash after "dir1."
Try:
RewriteRule ^(dir1|dir2|dir3)$ targetfile.php [QSA,L]
I think the problem is that the regular expression mandates a slash after the directory name (e.g. /dir1/), but in the example the last slash is omitted (http://www.somesite.com/dir1 does not have trailing slash).
I think you could try just with
RewriteRule ^/(dir[1-3]) /targetfile.php [R,L]

Categories