I'm using this rewrite rule, which works perfectly fine
RewriteRule ^(.*)-S\.html\??(.*)$ /index.php?mod=home&action=stadt&stadt=$1&$2 [QSA]
It points URLs like /Albersdorf-Holst-S.html to their PHP URL args.
Now, I need to rename some of these names, that's why I want to put RedirectPermanents in place. Likt this on for example:
RedirectPermanent /Albersdorf-Holst-S.html http://www.gruppenunterkuenfte.de/Albersdorf-Holstein--S.html
I would expect, when putting the RedirectPermanent in front of the RewriteRule it will be executed first and the RewriteRule will then be applied to the result.
But what happens, is that the URL in the adress bar reads after the redirect:
http://www.gruppenunterkuenfte.de/Albersdorf-Holstein--S.html?mod=home&action=stadt&stadt=Albersdorf-Holst&
Why is the query string added? And why does this only happen, when I redirect first.
Using RewriteRule ... [L,R=301] instead of RedirectPermanent helped:
RewriteRule ^Albersdorf-Holst-S.html http://www.gruppenunterkuenfte.de/Albersdorf-Holstein--S.html [L,R=301]
Related
I got this problem, where I try to make an Image Archive. Most of it works, but I have now come to prettifying URLs. I have worked with this before, but somehow, it don't work now. If I add more then one Rewrite URL to the same file (index), it ignores the values added with it.
What I got:
Header add "disablevcache" "true"
RewriteEngine On
RewriteBase /folder/
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^archive/([^/]+)?$ archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
The following should allow me to access these URLs:
domain.com/archive/category
domain.com/archive/category/subcategory/
domain.com/archive/category/subcategory/id
domain.com/archive/category/subcategory/id/item
domain.com/archive will be made default, since I use an index.php file, in a folder. And item will just be for SEO and has nothing to do with PHP. (The last rewriteRule)
This works. I can access the same page, from all the different URLs. (archive/index.php). But when I try to use PHP to get the value from the provided URLs, it won't work. I can't even check and see if there are values.
No matter what I do, it just echo out Category & Sub Category on every URL. Also if I go to the default URL, domain.com/archive.
if($_GET['cat']){
echo 'Category';
if($_GET['sub']){
echo 'Sub Category';
}
}
Anyone who can see what I am doing wrong or got a suggestion to why it ignore my values? Or maybe know another way to do this, so I end up with the same result?
Apache rewrites in a loop. You need to stop it for archive/index.php since that always matches the first RewriteRule:
RewriteRule ^archive/index\.php$ - [L]
RewriteRule ^archive/([^/]+)?$ archive/index.php?cat=$1 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
RewriteRule ^archive/([^/]+)/([^/]+)/([^/]+)?/([^/]+)$ archive/index.php?cat=$1&sub=$2&id=$3 [E=ORIG_URI:/$1]
You can also remove [E=ORIG_URI:/$1] if you don't use it.
My url is being redirected using .htaccess as follows:
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
Friendly url -> translates to php url
domain.com/b/hello/2 -> b/view.php?id=2&name=hello
BUT when someone comes to the site as follows:
domain.com/b/hello/2?query=xyz
I don't know how to get rid of the ?query=xyz
I have tried everything including [QSD] and I can't seem to get it to work.
Update
I have managed to get it to work with the following but it does two 301 redirects instead of one:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule ^b/(.*)$ /x/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)/([^/]+)?$ b/view.php?id=$2&&name=$1
Check if adding the question mark at the end of the rule will cancel appending query string from left side
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1? [QSD,L]
You can use an additional Rule to catch for GET parameters and strip them off.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
To make it only work on the /b/ subfolder, use this:
RewriteCond %{QUERY_STRING} .+
RewriteRule ^b/(.*)$ b/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
The first rule will redirect everything that matches your rule to the URL without any GET parameters (note the ? at the end of the rewrite rule, it will strip off the parameters).
The second rule will match in the case the first rule cannot be applied, i.e., when there are no paramaters
I have a problem (it's probably a simple one but I've never had the need to write regex)
A SEO specialist told me to make pretty URLs so I did with the .htaccess file the CMS provides.
But now he requires me to redirect the old URLs to new ones.
This doesn't work
RewriteRule ^index.php?page=kontakt$ /kontakt.html [R=301,L]
and also this (wich was supposed to redirect to the main page from the index.php file)
RewriteRule ^index.php$ / [R=301,L]
has resulted in sitename.com/?page=kontakt, so now I also have to redirect this.
How do I fix this?
RewriteRule only matches the base URL without the query string. You need an additional RewriteCond for it to work.
RewriteCond %{QUERY_STRING} ^page=kontakt$
RewriteRule ^index.php$ /kontakt.html [R=301,L]
EDIT:
Apparently query string gets preserved in this case, so you're probably getting /kontakt.html?page=kontakt
To discard original query string you need to put ? after URL.
RewriteRule ^index.php$ /kontakt.html? [R=301,L]
My htaccess redirect knowledge is somewhat weak, so I was hoping to get some help here.
I currently have the following redirect, which works well:
# remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]
# redirect to clean URL
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)$ /$1.php
This takes a URL like www.mysite.com/about to www.mysite.com/about.php
Now I would like to keep this behavior, but add parameters (if applicable), as such:
www.mysite.com/about => www.mysite.com/about.php
www.mysite.com/gallery/1 => www.mysite.com/gallery.php?id=1
If possible, I might like to expand this system to 2 or more parameters, as such:
www.mysite.com/gallery/1/2 => www.mysite.com/gallery.php?id=1§ion=2
So the pattern would be:
First URL segment redirects to a PHP file
(Optionally) the second segment gets added as the id parameter
(Optionally) the third segment gets added as the section parameter
Give it a try
# remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]
# redirect to clean URL
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)$ /$1.php
RewriteRule ^([a-zA-Z0-9_-\s]+)/([a-zA-Z0-9_-\s]+)/([a-zA-Z0-9_-\s]+)$ /$1.php?id=$2§ion=$3
#RewriteRule ^([a-zA-Z0-9_-\s]+)/([a-zA-Z0-9_-\s]+)/([a-zA-Z0-9_-\s]+)/([a-zA-Z0-9_-\s]+)$ /$1.php?param1=$2¶m2=$3¶m3=$4 and so on
You should consider using a generic router. This completely liberates your redirects to use whatever you want.
Typically a router rule looks like this:
RewriteRule ^(.*)$ handler.php?path=$1 [QSA]
You can add the -f and -d if you like to avoid sending static files (like CSS files and images) through your router, but in my case, even those pass through my router.
Then in handler.php you check the input, see if it's something you know how to deal with, like: is the URL like /about or is it like /gallery/[0-9].
I am using the following code to change all my pages from .php to a simple /
RewriteRule ^(.*)/$ $1.php [L]
Now come up as www.site.com/clubs/ or www.site.com/training/ instead of www.site.com/clubs.php and www.site.com/training.php
Now within clubs i want to show the clubs (using data from a database) and the page will show as www.site.com/clubs/my-club/
All the my-club data will be in the database including the link in a url field.
I can get this with the following code. However when i echo using the $_GET['club'] method it is showing up my-club.php instead of just my-club.
RewriteRule ^clubs/([^/]*)$ clubs.php?url=$1 [L]
The ending / in the url seems to get changed into .php
You can achieve what you need in different ways, however that's how I would do it.
Change rules order, like this:
RewriteRule ^clubs/([^/]*)/$ clubs.php?url=$1 [L]
RewriteRule ^(.*)/$ $1.php [L]
I added the trailing slash to the first rule, since I guess is part of the pattern. If I'm wrong about it you can just delete it.
If you want to append any query string from the original request URL you should use QSA together with L, so [L,QSA]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z\-]+?)/([0-9a-zA-Z\-]+?)?$ $1.php?url=$2
Whan are you run:
http://localhost/club/
result will be
http://localhost/club.php
Whan are you run:
http://localhost/club/my-club
result will be
http://localhost/club.php?url=my-club