mod_rewrite multiple directories to a php file - php

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]

Related

skip subdirectory when replacing underscore to hyphen from urls using .htaccess code

I have to replace _ (underscore) with -(hyphen) from urls for the SEO purpose.I have hundreds of files so I can't change filenames one by one.So I wrote some .htaccess code to do that.
This is the code I am using.
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
It successfully changing my urls from underscore to hyphen.
Now I have one subdirectory named "administrator" and I dont want this directory urls to replace from underscore to hyphen.
So I want to skip urls of this directory to skip from .htaccess code.
Please help me how to skip this.
Thanks!
You can also exclude separately:
RewriteCond %{REQUEST_URI} !^/administrator/
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
It's easier if you then need to exclude more than one directory, as it is just necessary to add the same line again.
You can use Negative lookahead:
RewriteRule ^(?!administrator/)([^_]*)_(.*)$ /$1-$2 [R=301,L,NC]
This will skip /administrator/ sub-directory from this rule.

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.

.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

.htaccess ModReWrite help

I am having some trouble with my ReWrite code. Please note that the .htaccess file is in the subdomain folder (...public_html/subdomain/ )
I am simply trying to rewrite a page request:
http://subdomain.mysite.com/home
http://subdomain.mysite.com/index.php?page=home
My .htaccess file looks like this...
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_])$ /index.php?page=$1
Does anything jump out at you?
Your current rule probably works for urls one character long (after the slash)!
Add a + to signify one or more characters, or a * for zero or more
Try
RewriteEngine On
RewriteRule ^/([A-Za-z0-9\-\_]*)$ /index.php?page=$1
If you want to use the rules in a .htaccess file, you need to strip the contextual per-directory path prefix from the RewriteRule pattern. If the .htaccess file is located in the document root /, you need to strip the leading /.
Additionally you need to quantify the character set. Otherwise it would only describe one character.
So try this rule:
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?page=$1
I think
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
is ok ;)

Categories