I am trying to rewrite my URL's to remove index.php? but I'm struggling a little to get it to work. The closest I can get is the answer here: remove question mark from 301 redirect using htaccess when the user enters the old URL
I need to convert the URLs to pretty URLs on the way out, and rewrite them back to the proper URL on the way in. The structure of the URLs is as follows:
https://sub.domain.com/index.php?/folder1/folder2-etc
Using the code from the referenced answer results in a double forward slash:
https://sub.domain.com//folder1/folder2-etc
The rewrite rules I'm using from the referenced answer are:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteCond %{THE_REQUEST} \s/+\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]
I suspect I know how to solve the first bit, but I'm struggling to understand the second rule for the internal forward.
Additionally, I'm wondering if this is the best way to do this. I'm currently running an Apache backend behind an Nginx reverse proxy. Would I be better doing the rewrite on the Nginx side and the internal forward on Apache?
EDIT:
Complication: I've noticed an additional structure to complicate things. Some URLs appear to have https://sub.domain.com/picture.php?/folder1/folder2-etc
For these, I'd be quite happy to keep 'picture' and just remove the .php? bit.
I'm guessing that for the first bit, Id need to do something like the following:
RewriteCond %{THE_REQUEST} \s/+index\.php\?/([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+picture\.php\?/([^\s&]+) [NC]
RewriteRule ^(.*)$ /picture/%1 [R=301,L]
But have no idea where to start with the opposite.... ie converting pretty urls back to standard. It would help if the following section could be explained to me?
^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]
RewriteRule ^/*picture/(.*)$ /picture.php?/$1 [L]
RewriteRule ^/*(?!/*index\.php$)(.*)$ /index.php?/$1 [L]
should do the trick. I wasn't able to test it yet though.
I only used the [L] last flag to stop applying rules on match. The QSA query string append flag doesn't seem to make sense as you don't seem to use ?key=value&... syntax anyway. Also dunno if you actually need the NC case-insensitive flag...
Side note:
I hope your php files don't serve paths with .. in them, as that would allow people to read arbitrary files from disk, e.g. /picture/../../../etc/passwd
Apologies, but as it turns out, the main reason I can't get anything to work is due to the use of relative URLs and dynamically generated links within the PHP. Not something I can change unfortunately. The not perfect URLs are something I'm going to have to live with. For reference, the app I'm using is Piwigo
Related
title pretty much surmises what I am trying to achieve,
this is a server only thing, no other intermediate languages but
Apache. The purpose of this is to render downloaded webpages that
have GET requests appended to them, and treat them like independent
web pages.
RewriteEngine On
RewriteRule ^(.*)$ /foo/bar/$1 [R=301,NC,L]
I know that this is possible as I did it by accident yesterday, but forgot to take notes on how it was accomplished.
You can try this rule for turning /index.asp%3fid=12345 internally into /index.asp?id=12345:
RewriteCond %{THE_REQUEST} \s/+([^?]*)\?([^=]+=[^\s&]+)
RewriteRule ^ /%1\%3f%2? [L,NE,R]
RewriteRule ^([^.]+\.(?:php|asp))[^=]+=(.+)$ /$1?id=$2 [NC,L,QSA]
Currently my URLS are horrible.
They are like:
http://www.racebooking.net/single_news.php?id=211
And i want them to look better and to be more SEO Friendly, like
http://www.racebooking.net/news/video-122.html
I am going to do it through Apache .htaccess. Surfing the web i found many different opinions about SEO. Some people say it's not good to use RewriteRule because it creates duplicated content and kills pagerank, but you have to send a 301 message.
Here comes the question: it's better to use
RewriteRule Pattern Substitution
or
RewriteRule Pattern Substitution [R=301,L]
to make my URLS look better without worsening my SEO?
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+single_news\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /news/%1.html? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^news/([^/.]+)/?$ /single_news.php?id=$1 [L,QSA,NC]
What is the difference in using ^ vs ^(.*)$ vs ^.*$ as wildcards in a RewriteRule?
My goal is to redirect http://carnarianism.com/ (anything) to the landing (default) page of http://carnarian.com/. I have found the following solutions, which all seem to work, so I wonder which is better for performance?
RewriteRule ^ http://carnarian.com/ [R=301,L]
RewriteRule ^.*$ http://carnarian.com/ [R=301,L]
RewriteRule ^(.*)$ http://carnarian.com/ [R=301,L]
All of these seem to work okay. This is my very first post on StackOverflow, most of the time I can find an answer just searching for it.
To be clear: ABOVE the questioned RewriteRule in my .htaccess is a RewriteCond and WWW Handler as follows:
RewriteEngine On
RewriteBase /
# FROM www. --TO-- NO www. See no-www.org
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} carnarianism\.com$ [NC]
########## The Above Questioned RewriteRule ??? ##########
RewriteRule ^ http://carnarian.com/ [R=301,L]
Note: I started this search with the following, but I did not want the following because the path was also passed, and I want it to go to the landing page only. Therefore, I know you need the parentheses to be able to use the $1 variable. I do not want the $1 variable.
RewriteRule ^(.*)$ http://carnarian.com/$1 [R=301,L]
^ makes none of the original URL accessible as backreferences. $0 is an empty string.
^.*$ makes the entire original URL accessible as the $0 backreference (so you can do e.g. http://example.com/oldurl.php?url=$0)
^(.*) makes the entire original URL accessible as both the $0 and $1 backreferences; it's usually used when you want to actually use the old URL in the replacement since it's more explicit about the use.
All of them match the same thing, but produce different backreference groups.
The one that is better performance wise is the one you have benchmarked yourself.
But since you are using a .htaccess file rather than having this configuration in the server directly (maybe via a VirtualHost?) which is parsed only once, it really doesn't matter. Parsing .htaccess files at every single request is much more time consuming than performing the regular expression by a factor of thousands.
If you care about performance you should never ever use .htaccess files and even disable their parsing with: AllowOverride None. Not disabling them, and having a request like: http://example.com/sites/css/theme/main.css Apache will still try to load all the following files:
.htaccess
sites/.htaccess
sites/css/.htaccess
sites/css/theme/.htaccess
It will generate system calls even if those file does not exist.
Trying therefore to improve your RewriteRule in an .htaccess file is like sneezing in the ocean in the hope of making it less salty. :)
Now, if you improved your setup to use server configuration and to answer your original question: ^.*$ might be more efficient than ^(.*)$ as less references needs to be created. Chance is high, however, that you can't measure it.
I currently have a site that has Drupal installed and it has clean urls so the .htaccess file contains the following:
RewriteRule ^ index.php [L]
In addition to this I want to be able to publish static html pages and have them use clean urls as well. I was thinking of differentiating them from the drupal pages by adding a specific keyword e.g. content and maybe having something like below (not sure if this will work) - where I get a url like www.domainname.com/nice-holiday and translate it to
domainname.com/ftp/pages/nice-holiday.html
RewriteRule ^content/(.+)$ domainname.com/ftp/pages/$1.html [L]
The problem is the first rule will try to execute against all requests. I have tried putting the more specific rule before the more general rule but it still doesnt work.
How can you have two mod rewrite rules based on a condition? e.g. presence of a particular word? and more generally has anyone had experience handling a CMS and static pages on the one website - or is that asking for trouble?
This is where RewriteCond comes in handy.
# make sure no rewriting is done for requests without www
RewriteCond %{HTTP_HOST} !^domainname\.com
RewriteCond %{REQUEST_URI} !^/?content/
RewriteRule ^ index.php [L]
# later on...
# don't want this rule to apply for non-www requests either
RewriteCond %{HTTP_HOST} !^domainname\.com
RewriteRule ^/?content/(.+)$ http://domainname.com/ftp/pages/$1.html [L]
I think this is what you're going for? You can eliminate the %{HTTP_HOST} conditions completely if you don't actually care about the www thing. The two rules can still coexist as long as you keep the %{REQUEST_URI} condition on the drupal rewrite, so drupal rewrites explicitly do not apply for URIs beginning with the /content/ prefix.
I'm trying to match a a bunch of redirects for my website with basically moved to a different folder on the server. I need to make http://www.site.com/index.php?page=anypage go to http://www.site.com/newfolder/index.php?page=anypage. The thing is http://www.site.com/index.php and http://www.site.com/index.php?page=home should remain untouched. How can I accomplish this?
I was trying the following in the .htaccess file, but I am affraid to make a mistake. I really don't know how to test this, either.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/index.php?page=(.*)$ http://www.site.com/newfolder/index.php?page=$1 [R=302,NC]
RewriteRule ^/index.php?page=home http://www.site.com/index.php?page=home [R=302,NC,L]
Now I figured that this is temporary, so I should know ho to reverse it! The next week, the links will have to redirect again to the root server. Also, what should I do to re-establish the normal redirection??
If I've followed your scenario correctly, you want something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !page=home
RewriteRule ^index.php /newfolder/index.php [R,L]
As far as testing goes, I prefer to try rules out on a local test server. If you have full control over the server (as is the case locally), there are some mod_rewrite directives that help you log what's going on, and that can be helpful in debugging. The module documentation has more information about this.
Edit: When you want to switch back, modify the RewriteRule above like so:
RewriteRule ^newfolder/index\.php /index.php [R,L]