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 ?
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.
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 am using codeigniter for my website, and before theres always that index.php? on my every url or links, for example
mysite.com/index.php?/about
Google has indexed all of my urls with that index.php? and I want to remove it and redirect it without that. I am having a problem rewriting the URL and redirect it to mysite.com/about and this what i have tried so far
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [QSA,L,R=301]
what happened is, it only removed the index.php,
for example mysite.com/index.php?/about will turn to mysite.com/?/about I don't know how to remove that question mark,
I'm not good on mod_rewrite thanks in advance for the help.
I think you can improve the rules slightly.
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} index\.php?\?.+
RewriteRule .*$ %{QUERY_STRING}? [R=301,L]
Essentially, you don't have to worry about the entire request line in %{THE_REQUEST}, which removes all the complicated regex. Also, the rule redirects to whatever is listed in %{QUERY_STRING}, and removes the query string.
I am not sure why you used QSA in the first place. I think that was part of the problem earlier. Just for an exercise, you can try removing QSA and see what happens.
You should try this one.
RewriteCond %{QUERY_STRING} ^/[a-z]+$ [NC]
RewriteRule ^/index.php$ %{QUERY_STRING} [NC,L,R=301]
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.