Forcing urls to rewrite/redirect? - php

This rewrite methode works, but its not Forcing urls to rewrite/redirect to the new urls.
I use this:
RewriteEngine on
RewriteRule ^page/([^/\.]+)$ search.php?q=$1
i can access rewrited urls (project/page/etc..) , but old urls/links (search.php?q=etc) still accessible without redirect.
note: i use $_SERVER variants to creating urls, and on localhost.

You have to rewrite project/page to search.php? in order to hide the ugly urls.
And redirect search.php? to project/page in order to make the canonical urls the only way to access that resource.
In your code there is no mention of the redirect, you're just rewriting.
Think of it this way
rewrite hides the ugly urls behind canonical urls (it hides them, it doesn't eliminate them);
redirect responds to the browser with a message like "search.php? has moved to project/page, try that link instead" and the browser follows the new link;

To redirect so called ugly URLs to SEO friendly URL you would need another Rewrite rule. Have your .htaccess code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^page/([^/\.]+) search.php?q=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\?q=([^\s]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
Once you have verified that it's working fine change R=302 to R=301.

You'd need a [R=301] on the rewrite rule to turn it into a client-side redirect. Otherwise it's purely an internal rewrite and the client will never see the url getting changed.
e.g.
RewriteRule ^page/([^/\.]+)$ search.php?q=$1 [R=301,L]

Related

How to redirect static URL

I am having a php html site hosted on server. I've url like http://www.test.com/sales.php when user click on the link of this url then it should show www.test.com/services/sales.php instead of www.test.com/sales.php.
I've tried following code but it is not working.
RewriteEngine On
RewriteRule ^http://www.test.com/services/sales.php$ http://www.test.com/sales.php [R=301,NE,NC,L]
You can use like that
htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^sales/? http://www.test.com/services/sales.php [R=301,L]
This is the more robust version that can also be used in the real http server configuration:
RewriteEngine On
RewriteRule ^/?sales\.php$ /services/sales.php [R=301]
RewriteRule ^/?services/sales\.php$ /sales.php [END]
If you want to have a clean URL ("https://example.com/services/sales" instead of "https://example.com/services/sales.php"), then this variant would make sense:
RewriteEngine On
RewriteRule ^/?sales\.php?$ /services/sales [R=301]
RewriteRule ^/?sales/?$ /services/sales [R=301]
RewriteRule ^/?services/sales/?$ /sales.php [END]
It is a good idea to start with a 302-redirection and only change that to a 301 once everything is working as expected. When using a 301 right away you might run into issues with caching, so you should always use a fresh anonymous browser tab when testing.

301 redirect & htaccess RewriteEngine conflicting

My site uses RewriteEngine to hide variables in the URI as follows:
RewriteRule ^([^/\.]+)/?/([^/\.]+)/?$ page.php?theme=$1&pg=$2 [L]
This works fine, but as we're relaunching, we've got a few 301 redirects to handle as well. I've added these below my rewrite rules, here's an example:
redirect 301 /about/about-the-site https://www.mywebsite.com/about-us
The problem is, that it's redirecting but adding extra crap to the url string:
https://www.mywebsite.com/?theme=about&pg=about-the-site
Any idea why this would be happening?
Don't mix mod_alias and mod_rewrite rules as these 2 modules execute at different stages. Keep everything in mod_rewrite itself:
RewriteEngine On
RewriteRule ^about/about-the-site/?$ https://www.mywebsite.com/about-us? [L,NC,R=301]
RewriteRule ^([^/.]+)/?/([^/.]+)/?$ page.php?theme=$1&pg=$2 [L,QSA]
Also note extra ? in the target URL to strip any pre-existing query string. Make sure clear browser cache while testing it.

htaccess redirect from asp to php with parameters

I have to redirect all my asp page to new website developed in PHP.
I had my asp page as,
abc.asp?id=82
Which need to be redirected to
siteurl/index.php/abc
Can any one help me with this in HTAccess?
I have tried with,
Redirect 301 /abc.asp?id=82 siteurl/index.php/abc
rewriterule ^/abc.asp?id=82$ siteurl/index.php/abc[R=301,L]
But this is not working and giving me a 404 error.
You can't match against the query string in either a Redirect or RewriteRule. It's not very clear what the scope you're trying to accomplish here. Is the id=82 important? Does abc mean "anything that's just letters"? Is siteurl a directory or a domain name? If it's strictly what you've attempted, then this is strictly how it'll work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=82($|&)
RewriteRule ^/?abc.asp$ siteurl/index.php/abc? [L,R=301]
You're making 2 main mistakes:
Rewrite rule matches only URI part and doesn't match query string
Rewrite rule in .htaccess doesn't match leading slash.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=82(&|$) [NC]
RewriteRule ^(abc)\.asp$ http://domain.com/index.php/$1 [R=302,L,NC]
Once you verify it to be working replace 302 (Temporary Redirect) with 301 (Permanent Redirect)
Just use
RewriteRule ^abc.asp$ siteurl/index.php/abc [R=301,QSA,L]
That should do the job.. Using the QSA flag will forward any GET paramater too, if that's what you meant with the title.

URL rewriting-code for rewriting

i am working for a site,which is in php.....i want to rewrite url
e.g www.3idiots.co.in/stories.php?id=17
if i want to rewrite it as
www.3idiots.co.in/stories/17.html
can any one tell me the code for this to write in .htaccess file.?
I'm assuming you're using Apache with mod_rewrite. Something like
RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
should do the trick. Of course you'll need to make sure that RewriteRule is allowed in that directory. See this wiki page for more information.
mod_rewrite can only rewrite/redirect requested URIs and not those that are in your HTML documents. So you should first make sure, that your PHP application is printing the correct URIs, so /stories/17.html instead of /stories.php?id=17.
After that, you can use the rule suggested by José Basilio:
RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1
Though redirecting requests of /stories.php?id=17 externally to /stories/17.html and then internally back to /stories.php?id=17 is possible, it’s not good practice as that would result in twice as many requests. But here’s the rule for that:
RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]

.htaccess mod_rewrite doesnt work as I (think) i configured it

I try using mod_rewrite but it doesn't seem to work, probally a stupid error on my side.
when i go to index.php?page=home i want to make the url cleaner, I get no error but it also doesn't work
this is in my .htaccess:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^page/([A-Za-z0-9-]+)/?$ index.php?page=$1 [NC]
The rule you have in htaccess only changes new links to the old ones ie /page/something to /index.php?page=something . This doesnt handle your old links.If you don't want your old links to be accessible you can redirect them to the new url format (cleaner version) using RewriteRule.
Add the following lines above your existing rules but bellow RewriteEngine on
RewriteCond %{THE_REQUEST} /index.php\?page=([^\s]+) [NC]
RewriteRule ^ /page/%1? [L,R]
To fix the css issue see this post
Seo Friendly Url css img js not working

Categories