RewriteRule in .htaccess is not working - .htaccess

I have a site where I need to show one URL for SEO reasons, but the actual landing page is a slightly different URL. I originally thought the requirement was to show the url which actually exists, but I was incorrect.
The required url: www.somesite.com/people/johndoe/?id=10
The url which actually exists: www.somesite.com/people/?id=10
I am trying this in my .htaccess file but to no avail:
RewriteRule ^/person/.+$ /person/$1 [R=301.L]
This doesn't appear to change anything and I am told my url doesn't exist.
I have looked up many similar questions on this site and elsewhere, but cannot find a solution that works. Thanks in advance
Edit (existing .htaccess):
# php_flag magic_quotes_gpc Off
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#AuthName "username"
#AuthName "username"
#AuthUserFile "/home/something/.htpasswds/public_html/passwd"
#require valid-user
AuthName "username"
AuthUserFile "/home/something/.htpasswds/public_html/passwd"
RewriteCond %{HTTP_HOST} ^sitename.org$
RewriteRule ^(.*)$ "http\:\/\/www\.sitename\.org\/$1" [R=301,L]

For doing internal rewrites use mod_rewrite rules with using R flag:
Use this code in your root .htaccess:
RewriteEngine On
RewriteRule ^(people)/.+$ /$1 [L,NC]

Your rewriterule condition cannot include a leading /. It is stripped off. Use
RewriteRule ^person/.+$ /person/$1 [R=301,L]
Also note that you had a period in the flags, not a comma.
Lose the " and \'s in the last rewrite rule:
RewriteRule ^(.*)$ http://www.sitename.org/$1 [R=301,L]
I don't know if you're going to get in any trouble with multiple 301s in action. It may depend upon the server configuration. Do you need for the revised URI to go back to the visitor and be seen in their browser? If not, lose the 301 on that rewrite rule.
Document what you're doing and why, with comments. For example, your last two lines are to force www.sitename.org instead of sitename.org.

Related

change structure of url to directory-like structure

I know this question has been asked and answered many times, but my experience of .htaccess, regualr expressions, mod rewrites etc.. have normally drove me crazy.
I see on most websites the structure of the url is in a directory-like structure, wwww.linku.biz/edit. My ultimate question is how do you do this?
Are all these sites behind the scenes have normal URL variables but just re-wrote? such as www.linku.biz/myprofile?edit="whatever" , is this all done with .htaccess, and mod_rewrites?
I want to type in my url www.linku.biz/search however its actually www.linku.biz/search.php
I want to type in my url www.linku.biz/JackTrow however its actually www.linku.biz/profile.php?us="JackTrow
Also I want data re-wrote when I have lots of URL data, such as www.linku.biz/search?a=1&b=2&c=3 actually beeing www.linku.biz/search.php?a=1&b=2&c=3
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 %{THE_REQUEST} ^[A-Z]{3,}\s/+search\.php\[?^\s] [NC]
RewriteRule ^ search? [R=301,L]
RewriteRule ^search/?$ search.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?us=$1 [QSA,L]
Please note that your requirement 1 & 3 will both be covered by rule # 1
Add this rules to your .htaccess file in Root folder :
To do this www.linku.biz/JackTrow <--- www.linku.biz/profile.php?us="JackTrow
RewriteRule ^profile.php?us=(.*)$ /$1 [R=301,L]
To do this : www.linku.biz/search however <--- www.linku.biz/search.php
RewriteRule ^(.*).php$ /$1 [R=301,L]
Your last one , i have not understood what you want exactly.

Using .htaccess to change directory in url

I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory

.htaccess RewriteRule seems not to work

I got a problem with my .htaccess here and altough I searched the web and tried many things, I could not find any solution...
I have a wordpress-installation with enabled permalinks. Additional to that, I need to rewrite another URL on this wordpress-installation, which does not belong to WP.
If a user browses to http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets, I'd like to show http://www.URL.com/jackets.html as URL. There should also be the possibility to directly browse to http://www.URL.com/jackets.html.
My .htaccess looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^jacket\.html$ http://www.URL.com/?page_id=30&tag=all&filterCategory=6\%23Jackets [L]
</IfModule>
# END WordPress
Does anyone have any idea why this does not work?
The WordPress rewrite rules are redirecting any requests to non-existant files through WordPress. The rewrite rule responsible for this, RewriteRule . /index.php [L], is marked with [L], indicating that no more rules are to be processed after it. In order for your rule to work, it must be placed before the WordPress rules, but after the line RewriteBase /.

htaccess rewrite for subdomain only

I'm trying to rewrite some parameters to beautiful links, but for a subdomain / a folder only. Unfortunately I can't get it to work, maybe also because there are some other rewrites in line before...
Heres my code:
<IfModule mod_rewrite.c>
# NON-WWW TO WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# WORDPRESS-BLOG
Options +FollowSymlinks
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# REDIRECT FOR SUBDOMAIN
RewriteCond %{HTTP_HOST} ^subdomain.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ index.php?cshort=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /index.php?cshort=$1&cid=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /index.php?cshort=$1&cid=$2&step=$3 [L]
</IfModule>
Basically only the last part is the one I want to rewrite to change URLs from something like
http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1
The other rewriting rules for www.example.com shouldn't get affected. Unfortunately my current codes only does the first two rules for the blog and the www, but nothing happens on the subdomain. What's wrong in my code?
When you say that you want to rewrite from http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1 you mean that you want the user to enter the pretty URL and to have it serve the full URL in the background, not that you want to redirect from the ugly to the pretty URL, right?
In your RewriteRules, what are you trying to accomplish with "(?:/)?"? As written, that doesn't make any sense to me. If you're just trying to match whether or not the directory path ends with a slash, you can do that as follows:
RewriteRule ^([^/.]+)/?$ index.php?cshort=$1 [L]
EDIT: Additional suggestions:
Move the "Redirect for subdomain" section above the "Wordpress Blog" section. Since the Wordpress rule applies to "everything that's not a real file or directory, regardless of domain" that should go last.
RewriteConds only apply to a single RewriteRule that follows them. For each of the three rules you have listed under "Redirect for subdomain", after updating them per the above suggestion, you need to repeat the two RewriteCond lines in front of the RewriteRule.

SocialEngine - 301 redirection of old site pages .htaccess

I installed SocialEngine to replace an old CMS. The problem is that a lot of old URLs are present in the search engines. I would like to redirect them to the corresponding URLs on the current website (because at this time they only redirect to the homepage).
All the content of my old CMS was imported into SE, so each old page has its equivalent now.
For example :
old address : /index.php?mod=news&ac=commentaires&id=367
new address : /articles/367
I tried to add in my .htaccess something like :
redirectmatch 301 index.php?mod=news&ac=commentaires&id=([0-9]+) http://www.nailissima.fr/articles/$1
But it doesn't work. When I type the old address, it still redirects to the homepage, not to the corresponding article.
Would you please help me to solve that ?
Here is my .htaccess :
# $Id: .htaccess 7539 2010-10-04 04:41:38Z john $
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
# sends requests /index.php/path/to/module/ to "index.php"
# AcceptPathInfo On
# #todo This may not be effective in some cases
FileETag Size
Thank you in advance !
Best regards
Possibly you have the problem with order of your redirects instructions.
First disable with comments all your current general rules and try your new redirects alone.
If they work, then attentivly think where you should place them.
[L] at the end of instruction makes it last, so it this instruction matches, not further will process.
Using this, write more detailed instructions first, and more general later, otherwise, you can meet situation when further instructions overright your current!

Resources