.htaccess URL redirection using User Agent - .htaccess

I've been trying to find a solution to my situation but none worked. I have the following htaccess file which does the following:
It redirects site.com/m/page to site.com/m/#page
I redirects site.com/d/page to site.com/m/#page
It deletes .php extension and the common_ pattern which I had in front of the pages
The code so far:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]
I would like the folowing:
a) If some user enters from an iPhone 'site.com/m/...' , continue (do nothing)
b) If some user enters from a desktop ar iPad 'site.com/m/page' to redirect him to 'site.com/d/#page'
I would really appreciate some help.

You can use a new rule for this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} !iphone [NC]
RewriteRule ^m/(.+)$ /d/#$1 [R=302,NE,L,NC]
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]

Related

mod_rewrite removes the ?page= fine, get &id= not working

I got my .htaccess working that it is correctly going from:
webshop/index.php?page=home to webshop/home
If I put the id in the URL it still needs to be webshop/product&id=1. What I want to create is webshop/product/1.
I searched a lot of options on the internet but could not get it working. The code below is my .htaccess file.
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Removes index.php from URL
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
# Rewrites /home to be /index.php?page=home
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^webshop/([^/]*)$ /webshop/?page=$1 [QSA]
Hope you guys can help me out.
You can use:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Removes index.php from URL
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrites /webshop/product/1 to be /webshop/index.php?page=product&id=1
RewriteRule ^webshop/([^/]+)/(\d+)/?$ /webshop/?page=$1&id=$2 [NC,QSA,L]
# Rewrites /webshop/home to be /webshop/index.php?page=home
RewriteRule ^webshop/([^/]*)$ /webshop/?page=$1 [NC,QSA,L]
You can change (\d+) to ([^/]+) if your id is not only a number

Pretty URL for .html with parameters

I have a website which consists only from .html frontend and I want to have pretty URLs. My goal is to create something like this
http://test.com/mypage.html => http://test.com/mypage
http://test.com/mypage1.html => http://test.com/mypage1
and for one specific page
http://test.com/my-prettlink.html?id=1 => http://test.com/my-prettlink/1
I tried this .htaccess which is located in project root but only removing .html works.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^my-prettylink/(\d+)*$ ./my-prettylink.html?id=$1
More specific rules should come before general rules. So, try:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subdirectory/
RewriteCond %{THE_REQUEST} ^GET\ /(subdirectory/my-prettylink)\.html\?id=(\d+) [NC]
RewriteRule ^my-prettylink\.html$ /%1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(subdirectory/.+)\.html [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^my-prettylink/(\d+)$ my-prettylink.html?id=$1 [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(?!js|css) %{REQUEST_URI}.html [L]
and also add <base href='/subdirectory/' /> in header

.htaccess pretty URL's excluding index.php and adding trailing / to the end

Ok, I've fought with it for hours. I have 3 different .htaccess scripts which do what I need, but I'm unable to mix them together.
Make a pretty url from (example.com/gallery.php -> example.com/gallery)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ $1.php
The script from #1 though forwards example.com/index.php to example.com/index, so this code removes index.php so example.com/index.php -> example.com
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
The script should add a trailing slash so example.com/gallery -> example.com/gallery/
# invoke rewrite engine
RewriteEngine On
RewriteBase /~new/
# add trailing slash if missing
rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
Can someone help me to combine those 3 scripts into one universal pretty URL scripts
add these directives to .htaccess in the root directory of your website
Options +FollowSymLinks
RewriteEngine On
# add trailing slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# rewrite gallery/ to gallery.php
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
# redirect example.com/index.php to example.com/
RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]

htaccess hide extension doesn't allow ExpressionEngine logins

So pretty much said it but, using this in my .htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## removes index.php
# Add/Hide index.php from everything except admin
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond $1 !admin$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
## hide .php extension
# To externally redirect foo.php to foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Will allow me to use /admin as a url, but will not allow me to log in,
If I remove this line RewriteCond $1 !admin$ [NC] it prepends index.php onto
/admin making it index.php/admin and I can't see the page
What are you trying to achieve? Just removing index.php? If so this .htaccess works:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then you'll have to access /admin.php of course, or you can put it in a directory named /admin if you wish.

301 Redirect is not working as expected

Trying to set 301 redirect in .htaccess file and here is what i am trying to do
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
RewriteRule (.*)$ http://www.mysite.com/wordpress/$1 [R=301,L]
I am testing this on my local machine using WAMPP server.Though when i hit http://localhost/wordpress/ i am getting redirected to http://www.mysite.com/wordpress/ but for other URL i am not getting redirected at all.for e.g
I have this URL in my local machine http://localhost/wordpress/2010/11/shadows/ and this at the server http://www.mysite.com/wordpress/2010/11/shadows/ but when i hit this URL i am not getting redirected to respected URL on the live server ,but i am being showed same page from local machine.
Working:
http://localhost/wordpress/
=> Redirected to:
http://www.mysite.com/wordpress/
Not working
http://localhost/wordpress/2010/11/shadows/
=> Redirected to:
http://www.mysite.com/wordpress/2010/11/shadows/
As clear from URL, I am trying to do this in Wordpress.
Here is complete .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
RewriteRule (.*)$ http://www.mysite.com/wordpress/$1 [R=301,L]
Can any one tell me whats wrong with the redirection entry? Thanks in advance
Update
I have even tried this option
Options +FollowSymLinks
RewriteEngine on
RewriteBase /wordpress/
RewriteRule ^(.*)$ http://www.mysite.com/wordpress/$1 [L,R=301]
Did not worked.
Always you first then the others (talking about RewriteRules only, man! :D ) (and you've forgotten the QSA directive).
So here's the "clean" version of your RewriteRule:
<IfModule mod_rewrite.c>
RewriteEngine On
# BEGIN My Own rewrite rules
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
RewriteRule (.*) http://www.mysite.com/wordpress/$1 [QSA,R=301,L]
# END My Own rewrite rules
# BEGIN WordPress
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
# END WordPress
</IfModule>
If it's in a .htaccess file then try without the / like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# BEGIN My Own rewrite rules
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
# Without the / after wordpress:
RewriteRule (.*) http://www.mysite.com/wordpress$1 [QSA,R=301,L]
# END My Own rewrite rules
# BEGIN WordPress
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
# END WordPress
</IfModule>
By the way this is the first time in many years that a find an "under construction" page nice!
Please tell me if it works.
%{HTTP_HOST] will contain something like localhost or www.thecolorsofmysoul.com. So your conditions will never match.
RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
and the redirecting to the external domain will never fire.
Also the first two rule act in consort to map any non-file/directory to index.php. http://localhost/wordpress/ has a regexp match string of "" so will fail the pattern "." and will fail through and will redirect with your "update". Try
Options +FollowSymLinks
RewriteEngine on
RewriteBase /wordpress/
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^.* http://www.thecolorsofmysoul.com/wordpress/$0 [L,R=301]
BTW with this base this should be in DOCROOT/.htaccess. The corresponding Wordpress rules (which shouldn't be above this redirect) are
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$) index.php [L]
You don't need to repeat the base in the target and the negative lookahead assertion removes the need for the first index.php rule.

Resources