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.
Related
I am trying to redirect all invalid urls to my index.php file via my .htaccess file.
Unfortunately I keep getting an Apache error.
My .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1
RewriteRule ^([A-Za-z0-9\s]+)$ index.php?p=$1 [L]
This invalid url shoud redirect to index.php:
/vacatures/jobapplication/facility-manager%20qsdf
But it throws the object not found 404 Apache error.
The rule you have which allows spaces does not allow hyphens. The rule you have which allows hyphens does not allow spaces. So anything which includes both will not match either.
Your invalid URL facility-manager%20qsdf includes both.
My guess is that your RewriteCond is supposed to apply to both rules, but that is not what is happening now, it will apply only to the first RewriteRule after it. You can solve all these problems by including just 1 RewriteRule, and amending it to accept everything you want:
RewriteRule "^([A-Za-z0-9\-\_\/\s]+)$" index.php?p=$1 [L]
Note that this requires at least one of the characters in your character class, in other words it will not match your "home" location when there is no path ("http://somewhere.com/"). If you want to also match for that location, change the + to a *, to allow 0 or more character matches.
Your rewrite rules do not match the url you indicated. Your REQUEST_URI is
/vacatures/jobapplication/facility-manager%20qsdf
I suspect the URL decoding is not done before the RewriteRule matching and therefore it's trying to match literally %20, yet % sign is not included in your match. I'm not sure why you're using two RewriteRules - why not do something like this?
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteCond %{REQUEST_URI} !^index.php(\?.*)?$
RewriteRule ^(.*)$ index.php?p=$1 [L]
This is the structure of my website:
website.com/events/beachparty?date=1224
website.com/events/poolparty?date=0101
website.com/events/boatparty?date=1105
There are lots of different pages all of which I would like to use the get feature on.
I want to rewrite the URL using htaccess so that they can be loaded like this:
website.com/events/beachparty/1224
website.com/events/poolparty/0101
website.com/events/boatparty/1105
Is this possible without having to create a separate rule for each page?
Thanks in advance!
Is this possible without having to create a separate rule for each
page?
Yes, you can use a regex, i.e.:
RewriteEngine On
RewriteRule ^events/page([0-9]+)/([0-9]+)/?$ /events/page$1?date=$2 [L]
The above will rewrite:
www.yoursite.com/events/page99/123
to
www.yoursite.com/events/page99?date=123
Notes:
([0-9]+) will match 1 or more (+) digits
The last forward slash is optional /?
$ means the end of the line (url)
[L] = Last, apache will stop processing further rules
Update based on your comments:
RewriteEngine On
RewriteRule ^events/([0-9a-zA-Z]+)/([0-9]+)/?$ /events/$1.php?date=$2 [L]
RewriteRule ^events/([0-9a-zA-Z]+)/?$ /events/$1.php [L]
[0-9a-zA-Z] - will match any digit or letter from a to z or A to Z, 1 or more times
Try...
RewriteEngine On
RewriteRule ^events/beachparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
RewriteRule ^events/poolparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
RewriteRule ^events/boatparty/([0-9]+)/?$ /events/beachparty?date=$1 [L]
I have problem in my URL.
This is my code in htaccess:
RewriteRule ^music-(.*)-([0-9_]+)\.html$ /artiste.php?g=$1&page=$2 [NC,L]
So some URL on Google or Bing could be showing like this music_(.*)_([0-9_]+)\.html
If possible I want to change _ to - with htaccess.
I want any url with _ to change to -, because all links work correct with - but in my research some URLs have _ so I want to replace them with -.
example:
Error : http://www.example.com/me_like_this.html
Correct : http://www.example.com/me-like-this.html
You can use the following in your /.htaccess file:
RewriteEngine On
# Replace underscores with hyphens, set the environment variable,
# and restart the rewriting process. This essentially loops
# until all underscores have been converted to hyphens.
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscore:yes,N]
# Then, once that is done, check if the underscore variable has
# been set and, if so, redirect to the new URI. This process ensures
# that the URI is rewritten in a loop *internally* so as to avoid
# multiple browser redirects.
RewriteCond %{ENV:underscore} yes
RewriteRule (.*) /$1 [R=302,L]
Then add your rule afterwards:
RewriteRule ^music-(.+)-(\d+).html$ /artiste.php?g=$1&page=$2 [NC,L]
If this is working for you, and you would like to make the redirects cached by browsers and search engines, change 302 to 301.
Note: In your RewriteRule I have changed .* to .+ so it only matches one or more characters, instead of zero or more characters. Additionally, I have changed [0-9_]+ to \d+, which is the shorthand equivalent without including underscores, which would be converted to hyphens anyway. If you want to include hyphens in the last capture group, then change (\d+) to ([\d-]+).
RewriteEngine On
RewriteRule ^(.*)_(.*)$ /$1-$2 [L,R=301]
RewriteRule ^music-(.*)-([0-9_]+)\.html$ /artiste.php?g=$1&page=$2 [NC,L]
Please try this.
I am trying to redirect my old pages to my new updated link structure pages because the old ones are now giving 404
Old
domain.com/artist-some-name
New
domain.com/artist/some_name.html
Where the - and _ between some name is put there instead of a space
So basically i want to replace the - seperator with the / which i can with
RewriteRule ^artist-(.+)$ /artist/$1.html [R=301,L]
But i can't work out how i can change the some-name to some_name so it will redirect properly
EDIT
RewriteRule ^artists-(.+)-(.+)$ /artist/$1_$2.html [R=301,L]
RewriteRule ^artists-(.+)$ /artist/$1.html [R=301,L]
Seems like this works for both if spaces in url or not
RewriteRule ^artist-(.+)-(.+)$ /artist/$1_$2.html [R=301,L]
Give these a try:
RewriteRule ^artist-([a-z]+)-([a-z]+)$ /artist/$1_$2.html [NC,R=301,L]
RewriteRule ^artist-([a-z]+)$ /artist/$1.html [NC,R=301,L]
You need to limit the wild cards so that only letters are included. If you need to include numbers ('digits' in regex), then you can add 0-9 just next to the a-z. Alternatively, you can use \d+, for short.
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.