I have a file in root directory named propertyid.php
I have a RerwiteRule that works OK:
RewriteRule for-sale/(.*) propertyid.php?pid=$1
User enters this URL and same URL displays in address bar
domain.com/for-sale/(pid)
I want a second rule that will allow users to enter this url
domain.com/sell/(pid)
but I want the first URL to display in the address bar
domain.com/for-sale/(pid)
I have tried multiple variations of the following
RewriteRule ^sell$ /for-sale/propertyid.php?pid=$1
RewriteRule sell/(.*) for-sale?pid=$1
You need to redirect /sell/(.*) to /for-sale/(.*). Your redirect rule of /for-sale/(.*) to propertyid.php?pid=$1 will take care of the rest.
Just check few things:
/sell/(.*) to /for-sale/(.*) rule should come above
/for-sale/(.*) to propertyid.php?pid=$1 redirect rule and should
return 301 http code for moved permanently.
Check for multiple redirects.
Your .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sell(.*)$ http://%{HTTP_HOST}/for-sale$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^for-sale/(.*)$ propertyid.php?pid=$1 [L]
Related
I have a particular scenario: the old site urls were made this way: http://www.mysite.it/it/my-old-link
The new one are made this way: http://www.mysite.it/vini.php?famiglia=my-new-link
I would like to the link http://www.mysite.it/it/my-old-link to point to http://www.mysite.it/vini.php?famiglia=my-new-link
This is to preserve SEO position. So, when someone click on search result http://www.mysite.it/it/my-old-link will be redirected to http://www.mysite.it/vini.php?famiglia=my-new-link
But I cannot find a way to do this editing the .htaccess file.
I've set a rewrite rule and a redirect but it doesn't work; here an example of the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule ^/?([^/d]+)/?$ /vini.php?famiglia=$1 [L,QSA]
Redirect 302 http://www.mysite.it/it/my-old-link http://www.mysite.it/my-new-link
ErrorDocument 404 http://www.mysite.it/index.php
But it doesn't work: if I enter http://www.mysite.it/it/my-old-link in the browser I get the 404 page error (index.php)
However: if I enter http://www.mysite.it/my-new-link on the browser I get the correct page.
Is there a way to solve this problem?
TIA tony
In case you are looking for specific rule as per shown samples then try following.
RewriteEngine ON
RewriteRule ^it/my-old-link/?$ vini.php?famiglia=my-new-link [NC,L]
In case you are looking for Generic rules, where if a URI is NOT present as a file or directory then try following. Please make sure either you put above or following rules only at a time.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ vini.php?famiglia=my-new-link [NC,L]
i am able to get subdomain variables (product name and language) with php via $_SERVER["HTTP_HOST"]
http://product.en.example.com
I do not know how to make to work in browser these urls for internal pages:
product.en.example.com/images
product.en.example.com/images/john
product.en.example.com/hobbies
product.en.example.com/hobbies/smith
images, hobbies = pageType
john, smith = peopleName
I think i should get the pageType via php query strings (GET) and serve the corresponding layout.
In .htaccess i have:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.(.+?)\.example\.com/$
RewriteRule (.+).(.+)\.example\.com\/(.*) /index.php [L]
There are multiple ways to achieve this. First of all you can parse the request uri in php using $_SERVER['REQUEST_URI'] which contains the requested URI.
An alternative is to use your .htaccess file. In this case you would not even have to parse the server name in php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(.*?)\.?([^\.]+?)\.example\.com$
RewriteRule ^([^/]*)?/?(.*?)/?$ /index.php?product=%1&language=%2&pageType=$1&peopleName=$2 [L]
Note that I added the rule RewriteCond %{REQUEST_FILENAME} !-f instead of your check against index.php because this way you can serve assets like your css files without having them redirected to index.php.
Beware that HTTP_HOST has no trailing slash.
EDIT
As requirested in the comments:
In order to add another optional parameter you can use ([^/]*)?/?. Therefore the RewriteRule could look like:
RewriteRule ^([^/]*)?/?([^/]*)?/?(.*?)/?$ /index.php?product=%1&language=%2&pageType=$1&peopleName=$2&peopleAge=$3 [L]
I have a rewrite rule for wildcard domains all requests are send to index.php on main domain. This is content for my .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.us$
RewriteRule ^/?$ "http\:\/\/domain\.us\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [NC,L]
And it works like it's supposed to work...perfect!
But recently I had to change the url structure of the whole website, that is fine old urls will simple show 404 though few old urls are important to me and I want to redirect them 301 to new url. For example:
somesubdomain.maindomain.com/oldcontent/
I want to reditect
somesubdomain.maindomain.com/importantdir/oldcontent/
Whatever I tried I ended up with a redirection loop (to many redirects at once).
I have like thousands of urls but only want to 301 redirect few of them.
My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.
I have a php generated file like:
http://www.domain.com/?mod=test&act=view
And I want to create a redirection from that address to something like:
http://www.domain.com/view-test
so that everytime a user (or bot) accesses the first uri it gets redirected to http://www.domain.com/view-test viewing the content of the first uri.
Right now I have the following rules under my .htaccess:
RewriteRule ^view-test$ /?mod=test&act=view [NC]
RewriteCond %{QUERY_STRING} mod=test&act=view
RewriteRule ^(.*)$ /view-test? [R,L]
The first rule creates a "page alias" and works if I delete the other two lines (but doesn't redirect my users as i want to)
After placing the last two rules I end up in a Loop or something and I get a broswer message saying "The page is not redirecting correctly"...
If I remove the first rule I get an 404 error saying /view-test could not be found
Any idea what I'm doing wrong?
I think that regex is better. Full .htaccess file for this situation if your php handler is index.php
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} mod=(\w+)&act=(\w+)
RewriteRule ^$ /%2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)-(\w+)$ /index.php?mod=$2&act=$1 [L]
forget about .htaccess it's easier to do it in php,i use it in my site, i created an index.php file to redirect users from example.com to example.com/123
in my index.php i put this
<?php
header('Location: http://example.com/123');
?
>