.HTACCESS Tricks - Rewriting for dynamic content - apache

I need to be able to make it so that when someone visits my site at say:
a) http://www.mysite.com/article/this-article/
It actually displays the content from:
b) http://www.mysite.com/article/index.php?src=this-article
and also if a user types in (b) then they are 301'd to (a)
In my HT access I currently have the following:
<IfModule mod_rewrite.c>
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]
# force www.
rewritecond %{HTTP_HOST} ^mysite.com [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.mysite.com%{REQUEST_URI}? [R=301,L]
</IfModule>

I'm not sure what you're describing is possible using only rewrite rules. But you can use a combination of rewrite rules and PHP like so:
Change your .htaccess file to look like this (NOTE: This is assuming your .htaccess file is in your root)
<IfModule mod_rewrite.c>
RewriteEngine On
# force www.
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC]
RewriteCond %{REQUEST_URI} !^/article/index(-new)?\.php
# this is looking for anything past article that is not a /. You may
# need to change that to be more inclusive - something like (.*)
RewriteRule ^article/([^\/]+) /article/index-new.php?src=$1 [L,NC]
# index.php to /
RewriteRule ^index\.(php|html)$ / [R=301,L]
#RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
#RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.mysite.com%{REQUEST_URI}? [R=301,L]
</IfModule>
Your article/index.php will become a redirector to your new index.php, and would look something like this:
<?php
if (isset($_GET["src"]))
header("location: /article/" . basename($_GET["src"])); // Using basename() in case src happens to be referring to an actual file in your code
else
header("location: /");
?>
Your "real" index would actually be index-new.php, which is what the rewrite rule would be calling.

Related

301 redirect doesnot work in htaccess

I want to redirect from https://*****.com/lp/index.html
to https://*****.com/lp/
so I put these two line in .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
and now the whole redirect block in my htaccess file is like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /lp/index\.html\ HTTP/
RewriteRule ^/lp/index\.html$ /lp/ [R=301,L]
but the index.html redirect not working.
Does anyone know why? Thank you.
Use it like this:
RewriteEngine On
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=301,NC,NE]
# remove /lp/index.html
RewriteCond %{THE_REQUEST} \s/+lp/index\.html [NC]
RewriteRule ^ /lp/ [L,R=301,NE]
Clear your browser cache and retest.
try this
http://www.rapidtables.com/web/tools/redirect-generator.htm
with Apache .htaccess redirect option

.htaccess basic url rewrites

There are a few things I want done to the URLs of my site that I cannot seem the .htaccess file to do.
1 remove file extension f.e. example.com/file.php should be example.com/file
2 remove the www. f.e. www.example.com should be example.com (I got this part to work, but I would hate it if after I put in fix and this no longer worked
3 no one should be able to see index.php at the end of root f.e. example.com/index.php should be example.com
4 my blog page should have nice urls f.e. example.com/blog.php?article=the-name-of-article should be example.com/blog/the-name-of-article
here is my current .htaccess file
rewrite URLs
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^blog/(.*)$ blog.php?article=$1 [QSA,L]
when I try to go to blog/the-name-of-article I get a internal server error.
From the body and comments of your .htaccess it appears that I would have provided it in the past :P
Only thing wrong in your .htaccess is ordering of rules. Always have them from most specific to most generic. Have your code like this:
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^blog/(.*)$ /blog.php?article=$1 [QSA,L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
To remove .php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
see Remove .php extensions with .htaccess without breaking DirectoryIndex
EDIT
For pretty URLs check this tutorial: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

Mod Rewrite with multiple parameters

I'm trying to setup a rewrite rule so that any text in place of a subdomain will be parameter one and any text after the first forward slash will be parameter two but I'm struggling with regex and am unsure about rewrite terminology.
For example, if someone requested:
joebloggs.mydomain.com
I would like them to see:
mydomain.com/index.php?site=joebloggs
Also, if someone requested:
joebloggs.mydomain.com/contact
I would like them to see:
mydomain.com/index.php?site=joebloggs&page=contact
By "see" I mean see the page, rather than see the URL - it's a CMS-like project so you can probably see where I'm going with it. Also, I've worked out how to remove www. so that's not an issue :)
EDIT
Current .htaccess is:
RewriteEngine On
# Remove trailing slash
RewriteRule ^(.+)/$ $1 [L]
# Remove www
RewriteCond %{HTTP_HOST} ^www.richardmjenkins.com$ [NC]
RewriteRule ^(.*)$ http://richardmjenkins.com/$1 [R=301,L]
# Rewrite for site/page pair
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^(.+/)?([^/]*)$ p.php?s=%1&p=$2 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^$ p.php?s=%1 [QSA,L,NC]
1. In your host panel : add a subdomain with name : * for enable all subdomains
2. this is your htaccess code :
RewriteEngine On
Options +Followsymlinks
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^$ index.php?site=%1 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?site=%1&page=$1 [QSA,L,NC]
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 %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^$ index.php?site=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^(.+)$ index.php?site=%1&page=%2 [L,QSA]

Simple URL Rewrite Problems

I'm having trouble re-writing a simple URL with .htaccess.
The URL I am trying to rewrite is:
http://www.domain.com/index.php?page=PAGE_NAME
I would like the PAGE_NAME to be directly after the domain, for example if PAGE_NAME is blog:
http://www.domain.com/blog
At the moment I have tried the following with no success:
RewriteRule ^/?([a-zA-Z0-9_-]+)?$ index.php?page=$1
All help is really appreciated.
Thanks in advance.
My current .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
ErrorDocument 404 /search.php?page=notfound
# Add WWW to URL
RewriteCond %{HTTP_HOST} ^cristianrgreco\.com$ [NC]
RewriteRule ^(.*)$ http://www.cristianrgreco.com/$1 [L,R=301]
# Remove trailing slashes from end of URL
RewriteCond %{HTTP_HOST} !^\.cristianrgreco\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Rewrite main page URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]
# Rewrite download URLs
RewriteRule ^download/([a-zA-Z0-9._-]+)/?$ download.php?file=$1
# Rewrite page navigation links
RewriteRule ^(.+?)/page-([a-zA-Z0-9_-]+)?$ $1.php?currentpage=$2
# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ articles.php?article=$2
# Remove file extension from PHP files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L,QSA]
</IfModule>
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#add these next 2 lines if you need to redirect http://www.domain.com/index.php?page=PAGE_NAME to http://www.domain.com/PAGE_NAME
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^\ ]+) [NC]
RewriteRule ^ %1? [L,R=301]
#send request to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,NC]

Redirect parked domain (htaccess)

I have two domains in the format of foo.com and foo.net. I currently have the following in my .htaccess to redirect non-www to www:
# Rewrite URL to force WWW
RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
RewriteCond %{SERVER_PORT} =443
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
However, I want to redirect URLs ending in .net to go to .com, including the subdomains.
Examples:
foo.net -> foo.com
www.foo.net -> www.foo.com
foo.net/bar -> foo.com/bar
sub.foo.net -> sub.foo.com
sub.foo.net/bar -> sub.foo.com/bar
I used to have this on another site, but it was years ago and I don't have the .htaccess anymore. Does anybody know an easy way of doing this?
Edit: Here is the rest of the .htaccess:
# BEGIN WordPress
#
RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive
# RewriteBase /
#
# if this request is for "/" or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|png|php|ico|css|js)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
RewriteRule . /index.php [L]
#
# END wordpress
# Rewrite URL to force WWW
-(see top of post)-
Ignoring the fact that this is by far the most devilish rewrite ruleset that I've ever written, this surprisingly seems to take care of what you currently have, plus what you want, in a nice, compact package.
Let me know if you have any problems with it:
RewriteCond %{HTTPS} =on
RewriteRule .* - [E=RW_HTTPS:s]
RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC,OR]
RewriteCond www.%{HTTP_HOST} ^(www\.[^.]*\.[^.]*)$
RewriteRule .* - [E=RW_THOST:%1]
RewriteCond %{ENV:RW_THOST} ^(.*)\.(net|com)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(.*)\.net$ [NC]
RewriteRule .* http%{ENV:RW_HTTPS}://%1.com%{REQUEST_URI} [R=301,L]
To Redirect Parked domain to main website domain with specific URL Try The following code on your htacess file :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^parked-domain.com [OR]
RewriteCond %{HTTP_HOST} ^www.parked-domain.com
RewriteRule ^(.*)$ https://main-domain.com/custom-url [L,R=301]