I have the following in my .htaccess currently-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^go/([^/]*)/([0-9]+)/([0-9]+)/?$ http://foo.com/wp-content/plugins/foo/cloak.php?post_id=$2&link_num=$3&cloaked_url=$0 [L]
RewriteRule ^go/([^/]+)[/]?$ http://foo.com/wp-content/plugins/foo/cloak.php?name=$1&cloaked_url=$0 [L]
</IfModule>
I want to change it so that the domain 'http://foo.com' is auto-detected and inserted (or just left off if it's unnecessary.
I'm hoping to use this .htaccess to manage multiple mapped domains to the same code base and can't really have it work with a specific URI in there.
Use RewriteCond for that:
RewriteCond %{HTTP_HOST} !^foo.com$
RewriteRule ...
Related
I cannot find the required regex command in order to do the following:
(1) Search a URL for a specific string (substring of a filename), i.e:
url.com/state/city_someotherdata.php
OR
url.com/state/someotherdata_city_xy.php and other variants of this format;
(2) convert the URL into this format:
url.com/state/city/index.php
What can be done to do this? I have tried this code
RewriteEngine On
RewriteCond %{QUERY_STRING} ([^\/?]+)\bozeman(?:\?|$) [NC]
RewriteRule ^http:\/\/url.com/montana/bozeman/? [L,R=301]
without success. This data is going into an .htaccess file.
Thanks in advance!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^state\/([^/]*)([^/]*|$) /state/$1_someotherdata.php [L]
</IfModule>
Let say you are working with Bozeman, Montana. This will do exactly what you need.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^.*(\/montana\/bozeman\/index.php)$
RewriteCond %{REQUEST_URI} ^.*(bozeman).*$
RewriteRule ^ /montana/bozeman/index.php [L,R=302,NE]
</IfModule>
You have to use a rewrite condition to exclude the URL you are redirecting to, otherwise the city name (Bozeman in this example) being the same as the folder being directed to, will cause an endless loop.
I have this link:
index.php/forums/viewforum/5/
Now, I want that "forums" word in URL to become dynamic such that which ever word I replace it with, it still redirects to the same URL.
For example, if I have:
ProductA/viewforum/5/
it redirects to:
forums/viewforum/5/
For example, if I have:
ProductB/viewforum/13/
it redirects to:
forums/viewforum/13/
In other words, if there's a "view forum" word in the URL, it should trigger this rewrite.
I already have a .htaccess that removes the index.php from the URL so the rewrite rule should consider that too.
Is it possible?
HTACCESS:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteEngine on
RewriteCond %{REQUEST_URI} (member)
RewriteRule ^(.*)$ http://%{SERVER_NAME}/404 [R=301,L]
RewriteRule ^[.*]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*)$ /home/ddco/public_html/index.php/$1? [L]
</IfModule>
You can use this rewrite rule:
RewriteCond %{THE_REQUEST} /forums/ [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
Also suggesting you to post your current .htaccess rules as well for further investigation.
I'm migrating a rather large (5000+ posts) from Movable Type to WordPress. At this point, I'm stuck trying to ensure that old post urls won't be result in 404s once we go live with the new site.
The old url pattern looks like so:
http://domain.com/site/category/category/post_name.php
And I'd like to redirect those to
http://domain.com/category/category/post_name/
However, I have tried and tried with htaccess redirects, and no matter what I do, it either fails or generates a 500 error. I suspect I'm missing something silly, or that there are conflicting rules maybe, and I'm hoping that someone who knows htaccess better than I do can help me along the right path.
Here's what I've got right now. The rule redirecting /site/ to the root directory works just fine, but the other two have no effect, whether alone or together. I tried both to see if I could redirect a specific post and do it manually that way, but it still won't work.
RewriteEngine On
RewriteRule ^site/(.*) /$1 [NC]
RewriteRule ^site/resources/(.*).php$ /resources/$1 [NC]
RewriteRule ^site/resources/research/safe_urban_form_revisiting_the_relationship_b.php$ /resources/research/safe_urban_form_revisiting_the_relationship_b/ [NC]
# 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
Any help would be extremely useful!
It looks like you may want to use a redirect something like this:
# Redirect /site/any/path/file.php to /any/path/file/:
RewriteRule ^site/(.+)\.php$ $1/ [NC,R=301,L]
Also, I would place this as the first rule immediately after the RewriteBase / line in the Wordpress section.
Since you´ll keep the same domain, why don't you just forget about writing the redirection rules yourself and use the redirection plugin instead? It will be much easier for you to define the redirection rules with the help of the plugin. This is the strategy I follow every time I can
The reason your redirects aren't working as expected is that . is a special character in Regular Expressions' syntax -- it means "any character". You need to escape any special characters like ., ^, etc. with a backslash like so: \..
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirect old URLs with ".php" in them.
RewriteRule ^site/(.+)\.php$ $1/ [NC,R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I'm not sure if you actually want the RewriteRule ^site/(.*) /$1 [NC] rule in there or if it was just testing. If you do, just add it in after the RewriteBase / statement.
I know that there is a lot of examples on Stackoverflow but I still miss something.
I'm trying to redirect http://old.domain.com/fr/ to http://brand.new-domain.com/fr/ with the following rules, but that doesn't work:
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [r=301,L]
# Remove index.php
# Uses the "exclude method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method
# This method seems to work best for us, you might also use the include method.
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
# Exclude root files
RewriteCond $1 !^(index\.php) [NC]
# Exclude EE folders
RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC]
# Exclude user created folders
RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC]
# Exlude favico, robots, ipad icon
RewriteCond $1 !^(favicon\.ico|robots\.txt|pple-touch-icon\.png) [NC]
# Remove index.php
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(URL=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
It correctly redirect when I call the root URL, but not when I call a page. What am I doing wrong?
Thanks in advance!
Pv
When writing mod_rewrite rules, the rules get applied in the order that they appear.
To redirect an old domain to a new domain, you'll want that rule to be first in your .htaccess or httpd.conf file — all other rules should appear after it.
If you only want to redirect a certain directory, the following rule will do so, while allowing the rest of the site to function normally:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Only Matching Directories
RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$
RewriteRule ^(.*)$ http://brand.new-domain.com/fr/$1 [R=301,L]
</IfModule>
If you want to redirect the entire site, the following rule will do so:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^old.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com$ [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
</IfModule>
If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, be sure to keep the 301 Redirect flag in the RewriteRule.
This will ensure that users and search engines are directed to the correct page.
While we're on the subject, as part of the EE 2.2 release, EllisLab now "officially" offers limited technical support for removing index.php from ExpressionEngine URLs.
Simply add or update your code to the following, making sure to consider any rules you may already have in place:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Try to use the following ruke as the first one:
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
Also mind the upper case R with is the short form for the lower case redirect.
Have you tried using mod_alias simple redirect instructions (a core module that you have), before trying the hacky-mod-rewrite thing?
I would do a VirtualHost with ServerName old.domain.com and in this VH I would add this rule:
Redirect /fr http://brand.new-domain.com/fr
from doc:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
So get a separate VirtualHost for brand.new-domain.com (with ServerName brand.new-domain.com) and in this one do not set the Redirect Rule.
If you still want to handle the 2 domains in the same VirtualHost then you'll have to use mod-rewrite as even RedirectMatch cannot check the request domain on the query.
I'm having trouble with this reg expression which i belive is correct, but it is not working.
What im trying to do is redirect bunch of urls containing a specific string like this:
http://www.example.com/**undesired-string**_another-string to http://www.example.com/**new-string**_another-string
and
http://www.example.com/folder/**undesired-string**/another-string to http://www.example.com/folder/**new-string**/another-string
So i have this code in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>
This should replace ANY undesired-string in any url to new-string, but it is not working, any idea why ?
Thank you
Marwen: Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)undesired-string(.*)$ yoursite.com/$1new-string$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ yoursite.com//$1 [L,R=301]
</IfModule>
In your 'updated' code in the comments above, you had it applying the rewrite condition to the undesired-string... So if the actual file or directory was valid it would not rewrite...
Doing this though will always rewrite the undesired-string with new-string - even if its a file name... If that is fine or what you want then all you had to do was move your rewrite conditions to below the rewrite rule...
also.. Just an fyi.. If everything is on yoursite.com you dont need to list yoursite.com
i.e.
yoursite.com/$1new-string$2
just needs to be
/$1new-string$2
which does the same thing: rewrites to the base directory of yoursite.com
now if they are going from mysite.com to yoursite.com then you woulud want to include the domain name because you are redirecting across domain names
Edit: You may also want to use:
[QSA,L,R=301]
instead of just [L,R=301]
Your regex is not really correct. Try:
RewriteRule ^(.*)undesired-string(.*)$ $1new-string$2 [R=301,L]
Or if this doesn't work, try:
RewriteRule ^(.*)undesired-string(.*)$ http://yoursite.com/$1new-string$2 [R=301,L]
Explanation:
^ marks the beginning; $ marks the end; the first (..) goes to $1, the second (..) goes to $2 and so on; * is 0 or more chars; + is 1 or more chars.
To answer my own question. Laravel already redirects the trailing slashes. Problem was that Laravel was installed into a sub-directory. I added the location of the sub-directory to the redirect. My location in this case is: "/lumen/public/". See the fixed htaccess below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /lumen/public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>