Have been going round in circles for a few hours, would really appreciate a little help.
I am trying to rewrite URLs that include a query string (preceded by company.php).
I found a few ways to get the rewrite itself to work, but it always shows a 404 :(
For example I need to rewrite (using example value):
http://test.domain.com/company.php?companynumber=05614768
to:
http://test.domain.com/company/05614768/
Here is a couple I have managed to get to do the rewrite but are 404ing:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/company\.php$
RewriteCond %{QUERY_STRING} ^companynumber=([0-9]*)$
RewriteRule ^(.*)$ http://test.domain.com/company/%1/? [R,L]
and
RewriteEngine On
RewriteCond %{QUERY_STRING} ^companynumber=([0-9]*)$
RewriteRule ^company\.php$ http://test.domain.com/company/%1/? [R,L]
I am guessing I need to figure out how do an internal redirect as well as the external redirect? But I am really struggling...
Centos 6.6 / Apache 2.4.12
Now if that's your old-to-new redirect:
RewriteCond %{QUERY_STRING} ^companynumber=([0-9]*)$
RewriteRule ^company\.php$ http://test.domain.com/company/%1/? [R,L]
Then remap the new incoming URL via:
RewriteRule ^company/(\d+)/?$ company.php?companynumber=$1 [END]
Note that this:
Must be a rule thereafter.
Should carry the [END] flag to avoid another internal rewrite round.
Only allows numeric \d+ placeholders.
See also "Ping-Pong rewrites" on Reference: mod_rewrite, URL rewriting and “pretty links” explained. These rewrites should just be used as temporary measure. Establish the new URLs right in your HTML templates.
Related
how can I write something like this:
RewriteCond %{QUERY_STRING} s=(\w+)
RewriteRule ^ wordpress/?s=%1 [L]
I need do rewrite rule from domain.com?s=something to domain.com/wordpress/?s=something.
Folder with wordpress is by symlink but It is not interesting.
Rule what I was send works but makes error 500 on other URLs like domain.com/something?company=1
Thanks
Btw: I must send this "body" twice for successful validation. Wtf #stackoverflow? And why are you cut greeting?
You just need this single rule:
RewriteEngine On
RewriteRule ^/?$ wordpress/ [L]
Original query string is automatically copied over.
My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.
First one is based on this question:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=1$
RewriteRule (.*) $1? [R=permanent]
After the answer below I modified the .htaccess file:
```RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule (.*) $1? [R=permanent]```
but it still did not work. Here is the debugging info:
RewriteCond %{QUERY_STRING} ^n=(.*)$
This condition was met.
RewriteRule (.*) $1? [R=permanent]
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
Second approach is:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ $1 [QSD]
None of them seem to work.
I have a few more questions:
Does rewriting only work if the URL is not typed manually?
Can I still access the values of query strings in PHP?
What happens when a user copies the rewritten URL?
The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.
And the other questions:
It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
What do you mean ?
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]
Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.
I have an empty .htaccess file
I have a current URL of http://www.website.co.uk/news.php?id=111111
I want this to become http://www.website.co.uk/news/111111
How Do I Do This?
Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111
http://www.website.co.uk/news/city/issue/the-title/111111
How can I make it so that the future changes will work too? So far I have:
RewriteEngine On
RewriteRule ^news/(.+)$ news.php?id=$1 [L]
This still displays the full url and typing in news/111111 redirects to an error page. Please help!
Adding the following to your htaccess should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]
RewriteRule ^news/(.*) news.php?id=$1 [QSA]
The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111
and below will change
RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]
RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111
The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.
In city=London, London will be contained in %1 etc
The second RewriteRule will allow you to find the id used.
I have re-written my URL from website.com?id=1 to website.com/1 and I'm getting 404 errors when trying to access the page and cannot think of a solution to this. I'm currently developing a link shortener. This is required so users will be able to access their shorted links.
This is my current .htaccessfile
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /(index\.php)?\?id=([0-9]+)([^\ ]*)
RewriteRule ^ /%3?%4 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /?id=$1 [L,QSA]
I cannot figure out whether this has something to do with the .htaccess file or if I need to add something else to my php code.
Would someone have some sort of idea? Thanks.
You need to explicitly rewrite back to index.php in your second rule. By the time rewrite rules are processed the DirectoryIndex directive has already been processed (or may never be processed at all - it depends a little on your virtual host configuration and in what scope the DirectoryIndex directive was declared).
The end result of this is that you need to explicitly rewrite the request to the script that you want to handle the request, you can't just rewrite it to the root of a directory. Try changing your second rewrite rule to:
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [L,QSA]
On a personal note, it's interesting to see someone else use the %{THE_REQUEST} approach to this problem, this is an idea that I myself only recently came up with, although presumably I am not the first to do so. For the benefit of future visitors, here is a related post that explains why this requirement would come about and the thinking behind it.
I think you have written wrong rewrite rules.
They must be something like this:
for example.com/website.php?id=x..
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^/]+)$
RewriteRule ^website\.php$ %1/ [L]
as discussed here: https://stackoverflow.com/a/4951918/2274209
Hope this will solve your query.