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.
Related
So, I'm currently building a REST API in PHP.
I managed to get slugs working for the most part.
If I request /api/admin/v1/users/1, it will return the user I need.
However, I also need to be able to add to it, e.g. /api/admin/v1/users/1/keys.
The HTACCESS file managing the slug is in the folder itself (/users/).
RewriteEngine On
RewriteRule ^(.*)$ user.php?slug=$1 [L]
I tried adding another line, but I think I messed up (I'm not that advanced with HTACCESS)
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [L]
This didn't do anything, it still returns the user object.
RewriteRule ^(.*)$ user.php?slug=$1 [L]
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [L]
The first rule matches everything, so the second rule is never processed. But since the first rule matches everything it will also rewrite itself (to user.php?slug=user.php) on the second pass by the rewrite engine.
You can resolve these issues by making the regex more restrictive. From your example URL it looks like the slug is numeric - in which case you can restrict the regex to match digits (0-9) only.
For example:
RewriteRule ^(\d*)$ user.php?slug=$1 [L]
RewriteRule ^(\d+)/keys$ keys.php?slug=$1 [L]
Note that the first rule also matches an empty URL-path, ie. no slug at all (as does your original rule). The second rule does not permit an empty slug (it would never match anyway).
The second rule don't work because the L flag stay for: last - stop processing rules
So you need to edit to:
RewriteEngine On
RewriteRule ^(.*)$ user.php?slug=$1 [QSA, L]
RewriteRule ^(.*)/keys$ keys.php?slug=$1 [QSA, 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'm trying to make pretty url but want to put content title in url instead id, then i put content title in url with query string:
index.php?action=content&id=22
changed to:
index.php?action=content&title=stack-over-flow
it's works fine. now i trying to make it pretty but got a problem in htaccess code.
before removing id in url code was:
RewriteRule ^([a-z]+)/([0-9]+)/?$ index.php?action=$1&id=$2 [NC,L,QSA]
then i changed to:
RewriteRule ^([a-z]+)/([a-z]+)/?$ content.php?action=$1&title=$2 [NC,L,QSA]
but it's not working and i will back 300 Multiple Choices page. well, i think it's a httaccess problem, but i'm new in htaccess, need a hand to fix this.
want this:
/content/stack-over-flow
Your regex is only allowing letters. It should also allow hyphen, numbers, upper case letters and underscore. Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/([^/]+)/?$ content.php?action=$1&title=$2 [NC,L,QSA]
I want to show URL like below examples:
1) http://www.domainname.com/detail/name/123.html
2) http://www.domainname.com/detail/124.html
In both URLs I want to show if "name" exist then want to display URL with "name" otherwise without "name".
1) RewriteRule ^detail/(.*).html$ detail.php?id=$1 [QSA]
2) RewriteRule ^detail/(.*)/(.*).html$ detail.php?id=$2 [QSA]
First rule is working file without "name". Second rule is not working and gives 404 for all pages.
Thanks in Advance.
You can use just one rule to handle both cases:
RewriteRule ^detail/(?:[^/]+/)?([^./]+)\.html$ detail.php?id=$1 [L,NC,QSA]
The problem you are having, is that the first rule matches both your first and second case. Obviously when id is name/123 your application can't handle it. What you want to do is limiting the characters to non-slash characters. After all, that means it can only match the last path segment. Besides that, force yourself to always escape literal dots in a regex. A dot matches pretty much anything if you don't do that...
RewriteRule ^detail/([^/]+)\.html$ detail.php?id=$1 [QSA,L]
RewriteRule ^detail/[^/]+/([^/]+)\.html$ detail.php?id=$1 [QSA,L]
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.