How to redirect static URL - php

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.

Related

Setting up specific page redirects as well as entire site redirects in .htaccess

I want to setup a redirect on my site to go to a different domain which I am able to do so with this rewrite rule:
RewriteEngine on
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]
This works great but there are also a few pages that I want to completely change the url as well as redirect to the new domain. Here is an example:
1.) Here is an example url to my old domain. https://www.olddomain.com/product-tag/[tag-name]/. I want this go to https://www.newdomain.com/parts?category=[tag-name]/.
2.) Here is an example url to my old domain. https://www.olddomain.com/product-categories/[category-name]/garbage. I want this go to https://www.newdomain.com/parts?category=[category-name]/.
I was hoping that adding something like this would do the trick but it does not seem to be working:
RewriteEngine on
RewriteRule ^/product-tag/(.*)$ https://www.newdomain.com/parts?category=$1 [R=301,L]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]
How can I achieve this?
Have it like this:
RewriteEngine on
# handle both specific URL redirects
RewriteRule ^/?product-(?:tag|categories)/([\w-]+) https://www.newdomain.com/parts?category=$1 [R=301,L,NC,QSA]
# redirect everything else
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L,NE]

Mod_Rewrite link to external url without parameters

i changed my cms and have now some old urls, that are still linked by other websites.
Now i have to redirect the old links to my new sites Start Page.
The old CMS was installed in a subfolder named "contentms". The new CMS is installed in the root-folder.
Old Urls look like: mywebsite.com/contentms/content.php?idcat=62
When i try to forward it using my rewrite rule:
RewriteEngine on
RewriteRule ^contentms/.*$ http://www.mywebsite.com/ [R=301,L]
It works, but there will appear:
http://www.mywebsite.com/?idcat=62
But i want to have only http://www.mywebsite.com, without the "?idcat=62" added to the external url.
Maybe you have a solution for me.
Thanks!
Give this a try and see how it works for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/contentms
RewriteRule ^(.*) http://www.mywebsite.com/? [R=301,L]
You can do this in the same line.
RewriteEngine on
RewriteRule ^contentms/.*$ http://www.mywebsite.com/? [R=301,L]

.htaccess URL rewrite works, but address bar then shows 'dirty' URL

User clicks link (from their email): http://www.site.com/edit/wih293f73y
Browser window opens and gets them to the correct page.
But now the browser's address bar shows: http://www.site.com/editor.php?editCode=wih293f73y
Extra info:
My rewrite rule is:RewriteRule ^edit/([A-Za-z0-9-]+)/?$ editor.php?editCode=$1 [NC,L]
This problem ONLY occurs when the user has clicked a link. It works perfectly when you just type the pretty url into the address bar.
This problem ONLY occurs for links that include the www. - the link http://site.com/edit/wih293f73y works like a charm.
My .htaccess file includes the following code (from HTML5 boilerplate, which I wasn't aware of previously):
# Rewrite www.example.com → example.com
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
If it's important, this occurs after my other rewrite rules.
I just took a look and it is apparent that your www rules is causing this. Question is do you want it be fixed? If you do then move this rule on top of all other rules and your problem should be fixed.
Move this to top of all other rules
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
You can use use the redirect directive
redirect 301 ^edit/([A-Za-z0-9-]+)/?$ editor.php?editCode=$1
There are some pros and cons to this strategy. The pros being;
It's super fast. You don't even need to load up your application for this to work.
It's minimal code.
Redirects are an intrinsic part of Apache (or any http server) and aren't going anywhere soon.
The cons being;
It's not part of your application proper. Should you decide to change logic or URLs, you'll have to change this, too.
It's slower, as you need to invoke php, as opposed to just having Apache issue the redirect.

Forcing urls to rewrite/redirect?

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]

Pretty URLs only partially working correctly

With a bit of help from people here at Stackoverflow I've managed to put together a .htaccess file that permits 'pretty URLs'. This is great if a user types the 'pretty URL' directly into the address bar as the conversion works exactly as I would like it to do, but if a user clicks a link within my site that generates a dynamic link, the 'ugly URL' remains and the conversion doesn't take place. Is there something I need to add to the .htaccess file to get this to work, or do I need to code up some PHP to force the conversion for links?
My .htaccess file is set-up as follows:
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^mysite\.com$
RewriteRule ^(.*) http://mysite.com/$1 [R=301,L]
RewriteRule ^episode/(0|[1-9]\d{0,2})$ /episode.php?episode=$1 [L,QSA]
(Converts http://mysite.com/episode.php?episode=31 to http://mysite.com/episode/31.)
Just append this rule in the end to force pretty URL in browser:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+episode\.php\?episode=(\d+) [NC]
RewriteRule ^ episode/%1? [R=302,L]
Once working change R=302 to R=301.

Categories