I have this rule in my .htaccess
RewriteRule ^([^/\.]+)/?$ ?page=user&id=$1 [L]
It rewrite a url like
http://sitename.ext/nickname
to
http://sitename.ext/?page=user&nickname
The problem is that with a url with dots like http://sitename.ext/nick.name.test i get a 404 error..
I'm not good with regex..
That's because it's not being rewritten. You specificaly told it to exclude ., and that's what it's doing.
Personally, I would favour something like this:
RewriteRule user/(.+) ?page=user&id=$1 [L]
If you want to match any character except a slash, the regex is [^/], since the \. will cause it also to not match dots.
Your rule should be
RewriteRule ^([^/]+)/?$ ?page=user&id=$1 [L]
You might find this site helpful.
Related
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.
I have a site that functions using a "language" URI attribute to set language
oursite.com/home?language=en
We need to be able to also use SEO friendly 2 character URI preppenders
oursite.com/en/home
I'm currently doing a redirect to the index.php file in .htaccess with a few exceptions so my .htaccess has these 2 lines for rewrites
RewriteRule ^([a-z]{2}|[a-z]{2}-[A-Z]{2})/(.*)? ?$2/language=$1
RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?/$1
I need to do a logical rewrite to end up with
/index.php?/home?language=en
What would be the correct Rewrite rule set to make this happen? Is it really even possible?
The RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?/$1 line isn't going to work because you can't create a backreference to a negative match.
Also, a request like: /index.php?/home?language=en is kind of ambiguous, the ? is reserved and needs to be encoded in the query string, otherwise, it can be appended (so that ? becomes a &). Try something like:
RewriteRule ^([a-z]{2}|[a-z]{2}-[A-Z]{2})/(.*)? /$2?language=$1 [L]
RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?%{REQUEST_URI} [L,QSA]
This takes: http://oursite.com/en/home and rewrites it internally to the URI /index.php?/home&language=en. But if you literally want an encoded ? in the query string then change the second rule to:
RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?%{REQUEST_URI}\%3F%{QUERY_STRING} [L,NE]
You could do this in the first rule.
RewriteRule ^([a-z]{2}|[a-z]{2}-[A-Z]{2})/(.*)? /index.php?/$2?language=$1
As for the second rule (for the urls without a language prefix), you could do something like this. As like Jon said, you can't backreference a negative match.
RewiteCond $1 !^index\.php|robots\.txt|sitemap\.xml|robots\.txt
RewriteRule (.*) /index.php?/$1
I'm using URL rewriting with Wamp but I don't find the correct regex for my needs.
I'd like to transform http://localhost/site_artisans/site_artisans/peintre-annecy.php in http://localhost/site_artisans/site_artisanspeintre-annecy.php (remove the slash between site_artisans/site_artisans and whatever is after).
I thought of :
RewriteEngine on
RewriteRule .*site_artisans/site_artisans/.* site_artisans/site_artisans [L]
(Unknown number of characters before and after and slash removed).
But this doesn't work.
Try
RewriteEngine on
RewriteRule .*site_artisans/site_artisans/(.*) site_artisans/site_artisans$1 [L]
I tested it on http://martinmelin.se/rewrite-rule-tester/ and I believe the result is what you want.
Try this:
RewriteEngine on
RewriteRule .*site_artisans/(site_artisans/.*) site_artisans/$1 [L]
It's a part of my htaccess:
RewriteRule ^post/(.*)$ post.php?name=$1
RewriteRule ^(.*)/$ cat.php?name=$1
The URI will be somthing like this:
www.domain.com/category-name/
www.domain.com/post/hello-world
as you see, in the end of the first address (category) there is a '/' and on the second there isn't '/', how can I do it too on the second address? if I will do somthing like this:
RewriteRule ^post/(.*)/$ post.php?name=$1
it won't work because the server 'thinks' that I mean to category address.
hope you understand thank you.
Use the [L] modifier on every rule so it stops processing further rules when a rule matched. In this case you can even make the trailing slash optional!
RewriteRule ^post/(.*)$ post.php?name=$1 [L]
RewriteRule ^(.*)/?$ cat.php?name=$1 [L]
Try using (.*?) instead of (.*).
Without the question mark, it's "greedy" in that it will match everything up to the final match on the line. With the question mark, it will only match up to the first match on the line.
Make the slash optional (both times):
RewriteRule ^post/(.*?)/?$ post.php?name=$1 [L]
RewriteRule ^(.*?)/?$ cat.php?name=$1 [L]
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]