Hot answers tagged

12

Yes, if you have your URLs redirected properly. Yes. You should be using a 301 redirect instead of internally rewriting the URLs. No, this is completely unnecessary. If your old URLs are 301 redirected to the new URLs, then Google will know that those resources have been permanently renamed/moved. That's the whole point of having a 301 redirect code (as ...


10

Try this RewriteEngine On RewriteBase / RewriteRule ^edit/id/([0-9]+)/?$ edit.php?id=$1 [NC,QSA,L]


9

You are looking for the Query String Append flag - e.g. RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^search/(.*)$ search.php?q=$1 [QSA] See the mod_rewrite documentation for a full description of RewriteRule flags.


9

For your first two rewrites (non-www → www, http → https), the following rule should work: RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !=www.example.com [NC] RewriteRule ^(.*) https://www.example.com/$1 [NS,L,R=permanent] Just replace www.example.com with the actual canonical hostname of your site. As for your third ...


8

To implement such redirect using mod_rewrite and .htaccess you need to use RewriteMap directive which cannot be placed in .htaccess -- only in server config / VirtualHost context. If you have such access: 1. Place this line inside <VirtualHost> block for your site: RewriteMap lc int:tolower 2. Place this in your .htaccess: RewriteCond ...


8

It means "this matches the start of the string". So in your example the rule does the rewrite if there's the start of the string (^) any character (.) any number of the previous character (*) the end of the string ($) In other words, this whole expression matches any string.


7

Thanks for your time to look at the question, but we appeared to have figured it out: Options -Multiviews -Indexes +FollowSymLinks RewriteEngine On RewriteBase / DirectorySlash Off # remove trailing slash RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L] # rewrite /dir/file?query to /dir/file.php?query RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 ...


6

The way that you will do the 301 redirect does not play any role for the Google. Actually it does not see your configuration file. What it is important is to send the 301 headers with the new location on each old url and not just the home page. For example if you have 10 pages on the old website, make sure that these all pages are redirected to the new 10 ...


6

Just redirect by 301 HTTP status all your old URLs to the new ones. If you only change file extension, you can do it easily with an .htaccess file (if you use Apache as a web server). Put these lines in your .htaccess file: RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php Of course, change www.example.com by your domain name. By using 301 ...


6

Assuming your .htaccess is on the right place with the right permissions and your Apache server is configured to use it and mod_rewrite is enabled: RewriteEngine on RewriteCond %{HTTP_HOST} ^www.example.com$ RewriteRule ^/?(.*)$ http://example.com/$1 [R=301,L] Note the HTTP_HOST vs HTTP_POST in your example and that I use a 301 redirect. This is better ...


6

So I wrote a set of Rewrite rules that did what you wanted, but it completely broke my website. I realized that what you want is probably not what you need. Adding trailing slashes to the end of all URLs really messes with the semantics of the URL in that you're no longer accessing the file /foo but the content listing of the directory /foo/. For example: ...


5

What I do: use vhosts for each site, including a vhost for the variant I want to suppress. This keeps all the configuration for a named site in one place. The duplication of content is minimal thanks to mod_macro. You want to issue a redirect, not rewrite internally, since the idea is to get the client to retry using the correct protocol. Make sure to only ...


5

The first line is used to prevent internal URL's from being rewritten. That might cause different pages to be displayed, so I've removed it. If the host is example.com or is not requested over HTTPS, it will be rewritten to https://example.com/. I.e., it rewrites http://example.com, http://www.example.com and https://example.com to https://www.example.com ...


5

The httpd.conf for campaign.com with it pulling data from example.com/macguffin/ <VirtualHost *:80> ServerName campaign.com <Proxy *> Order deny,allow Allow from all </Proxy> ProxyRequests Off ProxyPreserveHost Off ProxyPass / http://example.com/macguffin/ ProxyPassReverse / http://example.com/macguffin/ <Location /> ...


5

The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately. In other words -- RewriteRule cannot be used to match query string directly -- only with help of RewriteCond. Considering the ...


5

You need to add the following line into your .htaccess file(s): Options +MultiViews The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a ...


5

Place this in your .htaccess file: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /blog?display=$1 [L] This will redirect each URL which do not corresponds to an existing file (!-f) or existing directory (!-d) to the corresponding blog?... URL. [L] makes the rewriting stop here (in case you have ...


5

I will go onto something like that. First you define a new condition (RewriteCond) then you apply this condition using a rule (RewriteRule). Thanks to the flag L at the end of the RewriteRule, this stop the rewriting process immediately and don't apply any more rules. So when the request is a .css file, the first rule will be apply and not the second one. ...


5

You shouldn't need to use percent encoding/hexcode in mod_rewrite parameters since mod_rewrite will encode special characters (?, #, , &, etc.) by default. To write a space in the rewrite pattern, use \s or just escape the space with a backslash (\). Whether space gets encoded as %20 or + depends on whether it's part of the query string or the URI. So ...


4

Finally realised what the problem was when, after deleting the tag in both files, the error "Invalid command '\xef\xbb\xbf EF BB BF = BOM Moral of the story: Always check your encoding* and hex dump of your files when you get weird issues. *On Notepad++ you can use the Encoding -> Encode in UTF-8 without BOM option to remove it


4

This rule will redirect (301 Permanent Redirect) all incoming requests to xxx.yyy.site.com domain to http://xxx.site.com/ preserving the URL path (e.g. http://xxx.yyy.site.com/kitten?say=meow will be redirected to http://xxx.site.com/kitten?say=meow: RewriteEngine On RewriteCond %{HTTP_HOST} =xxx.yyy.site.com RewriteRule .* ...


4

You can use one of these solutions: Keep the Current .html extension unchanged for just frontend purpose and use Apache .htaccess file to proxify your requests so that. Add this to your .htaccess file to work: RewriteEngine on RewriteRule ^(.*).html $1.php OR Why not just have the HTML pages parsed as PHP? That way you can get the best of ...


4

You're almost there. Drop the very first RewriteCond (as it's not required and won't match ever) and add a trailing / to your last RewriteRule since you're redirecting with them. RewriteEngine On RewriteCond %{HTTP:Accept-Language} ^en [NC] RewriteRule ^$ http://mysite.com/en/ [L,R=301] RewriteCond %{HTTP:Accept-Language} ^de [NC] RewriteRule ^$ ...


4

Try either RewriteRule ^first-page/$ /subdir/page.php?id_page=1 [QSA,L] RewriteRule ^second-page/$ /subdir/page.php?id_page=2 [QSA,L] RewriteRule ^third-page/$ /subdir/page.php?id_page=161 [QSA,L] or RewriteRule ^first-page /subdir/page.php?id_page=1 [QSA,L] RewriteRule ^second-page /subdir/page.php?id_page=2 [QSA,L] RewriteRule ^third-page ...


4

Your regular expression has the following problems: It only has .* for the "site" and "sectionname", but is missing one for the "categoryname". Based on your example, there should be a third directory level in your regex. You have parenthesis around items which you don't need to reference, but no parenthesis around the ID, which you will need to ...


4

If you are using Wordpress now, and simply trying to redirect old Joomla urls to your new urls I suggest using a plugin to make your life easier. I recommend this plugin a lot: http://wordpress.org/extend/plugins/redirection/ Also keep in mind that you should use fancy permalinks within Wordpress instead of the standard ID numbers. You can find this ...


4

This should work for files of any extension: RewriteEngine on RewriteCond %{QUERY_STRING} ^/(.*\..*)$ RewriteRule ^index\.php$ http://www.newsite.com/%1? [L,R=301] Or to redirect the entire path and file request: RewriteEngine on RewriteCond %{QUERY_STRING} ^/(.*)(\..*)$ RewriteRule ^index\.php$ http://www.newsite.com/%1%2? [L,R=301]


4

RewriteEngine On RewriteRule ^(.*)\.abc$ $1.php that should change all of your pages from .PHP to .ABC



Only top voted, non community-wiki answers of a minimum length are eligible