Generic rules to rewrite urls [closed] - .htaccess

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to rewrite my urls for my website.
I've 5 pages and for all of them, I need to insert a new RewriteRule like this one:
RewriteRule about$ about.php [L,QSA]
RewriteRule contact$ contact.php [L,QSA]
RewriteRule ^blog/([^/]+)$ article.php?i=$1 [L,QSA]
Is there a way I can make it more generic ?
Thanks.

You probably are looking for something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,END]
RewriteRule ^/?blog/([^/]+)$ /article.php?i=$1 [QSA,END]

Related

How to redirect index.php to root in htaccess? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Actually i want to make auto redirect when open
from
test.com/login.php
to
test.com/login/
What should i put on my htaccess file.
You'd first redirect to pretty-url if the raw request matches:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /login\.php [NC]
RewriteRule ^login\.php$ /login/ [R=301,L,NC]
Now, you deal with the rewritten url to internally redirect to correct page:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^login/?$ /login.php [NC,L]
Have you tried something?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index/?$ index.php [NC,L]
Your rewrite module should be enabled for doing this?

Redirect all requests under a domain to an under construction folder [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have been trying to redirect all requests under a domain to an underconstruction folder with the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/underconstruction/
RewriteRule ^ /underconstruction/ [R=301]
But it doesn't seem to work.
I tried this (and it works):
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/underconstruction/underconstruction.html
RewriteRule ^ /underconstruction/underconstruction.html [R=301]
But I don't see the images and CSS that it comes with it.
Does anyone have any idea?
Maybe one solution is to exclude all image and CSS files from the rule, like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Add or remove file types in the next line if necessary.
RewriteCond %{REQUEST_URI} !\.(css|jpg|png|gif|bmp|js) [NC]
RewriteCond %{REQUEST_URI} !underconstruction\.html [NC]
RewriteRule .* /underconstruction/underconstruction.html [R=302,L]
Other options are to replace relative with absolute paths in the links to those files or to use the BASE element as described in this answer

How do I make sure my site always has the https infront [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Hey guys I have the following .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.site.com$ [NC]
RewriteRule ^(.*)$ https://www.site.com/$1 [L,R=301]
This will always make sure my ssl is properly showing, but for some reason on google if you are to search the site and than click on its subcatagorys, like services. It just takes you to site's services and displays it without the https, so it just becomes www.....
How do I fix this?
David
the rewritecond you're using is preventing the urls with www (eg http:// www.site.com/page.html) from being redirected
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.site.com%{REQUEST_URI} [R=301,L]

Virtual sub domains plus additional parameters [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have the following rules which works fine. It simply give me the ability to make virtual sub domains:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /projects/index.php?project=%2 [L,QSA]
When it's project.domain.com it's totally okay, however, I need to cover up project.domain.com/first/ to domain.com/index.php?project=%2&a=first and project.domain.com/first/second/ to domain.com/index.php?project=%2&a=first&b=second.
I've already tried couple of things, but none of them worked. I can list them but that's gonna be a little bit long with all the description. If anyone could help, that's great, otherwise please comment to update my question with whatever I've already tried and their results.
You may try this:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?([^/]+)?/? /projects/index.php?project=%2&a=$1&b=$2 [L,QSA,NC]
Option
If the redirection is to:
http://domain.com/index.php?project=%2&a=first&b=second
as in the last example, replace the previous rewrite rule with this one:
RewriteRule ^([^/]+)/?([^/]+)?/? http://domain.com/index.php?project=%2&a=$1&b=$2 [L,QSA,NC]

.htaccess RewriteRule [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I want
http://mysite.com/?p=47&preview=true
to be rewritten to
http://mysite.hostingcompany.com/?p=47&preview=true
But all other requests to http://mysite.com/ to act normally
My .htaccess looks like this:
RewriteCond ^mysite.com\/?p=(.*)\&preview=true$
RewriteRule ^mysite.hostingcompany.com/?p=$1&preview=true$ [L]
But it's not matching. I must be butchering my regular expression but I'm not sure how.
RewriteCond matches against a variable like %{HTTP_HOST} or %{QUERY_STRING}. In your case, you can make use of both of them:
# If the requested host is mysite.com
RewriteCond %{HTTP_HOST} ^mysite\.com$
# And the query string matches
RewriteCond %{QUERY_STRING} p=(\d+)
RewriteCond %{QUERY_STRING} preview=true
# Redirect it, appending the querystring as is
RewriteRule (.*) http://mysite.hostingcompany.com/$1 [L,R,QSA]

Resources