htaccess - how to capture the current rewrited url? - apache

I want to capture the current rewrited url via .htaccess in order to create to a custom HTTP Link header.
the script url :
http://localhost/index.php
the rewrited url :
http://localhost/news
the htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)$ index.php?&m=$1 [NC,L,QSA]
RewriteRule ^(.*) - [E=ORIGPROTO:http]
RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}]
RewriteRule ^(.*) - [E=ORIGQRY:%{REQUEST_URI}]
the output:
ORIGPROTO = http
ORIGURI = localhost
ORIGQRY = index.php
I am expecting ORIGQRY to be equal to 'news'
My question is how to capture the current rewrited url via htaccess?
EDIT 1
RewriteRule ^(.*) - [E=ORIGPROTO:http]
RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} ^(.*)[^/]$
RewriteRule . %1/ [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)_(.*)$
RewriteRule . %1-%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\º(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\!(.*)$
RewriteRule . %1 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\,(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\'(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\"(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\“(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\”(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\((.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\)(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\[(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\](.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\{(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\}(.*)$
RewriteRule . %1%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\:(.*)$
RewriteRule . %1-%2 [R=301,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)\+(.*)$
RewriteRule . %1-%2 [R=301,L]
#avoid uppercase & accented
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)(A|Â|â|Æ|æ|À|à|Å|å|Ã|ã|Ä|ä)(.*?)$
RewriteRule . %1a%3 [R=301,L]
.
.
.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ index.php?&m=$1 [NC,QSA,E=ORIGQRY:$1,L]
#404 page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pt=404&acc=404_page [L]
Header always set Link "<http://cache_api.com/%{ORIGPROTO}e://%{ORIGURI}e%{ORIGQRY}e>;rel=timegate"

Rules are executed in the order of their presence in .htacces. Here your first rule is modifying REQUEST_URI to /index.php.
FIX: Change the order of your rules:
RewriteRule ^(.*) - [E=ORIGPROTO:http]
RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}]
RewriteCond %{THE_REQUEST} \s/+([^\s?]+)
RewriteRule ^ - [E=ORIGQRY:%1]
Header always set Link "<http://cache_api.com/%{ORIGPROTO}e://%{ORIGURI}e/%{ORIGQRY}e>;rel=timegate"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ index.php?m=$1 [NC,QSA,L]

Related

The Forbidden rule in htaccess using OR doesn't work

This code correctly returns 403 error for URLs that don't end with / and have string name view-ports
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{THE_REQUEST} !view-ports
RewriteRule ^ - [F]
I'm going to add more strings like view-ports into the code so I tried this code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{THE_REQUEST} !view-ports [OR]
RewriteCond %{THE_REQUEST} !string-2 [OR]
RewriteCond %{THE_REQUEST} !string-3
RewriteRule ^ - [F]
The code above is not working, it returns 403 error for all URLs.
I managed to find the answer, here it is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !(view-ports|string-2|string-3)
RewriteRule ^ - [F]

WAMP nice URL's redirect to direct file

Hello when I trying nice url with htaccess it's redirecting to file, so when I type www.page.com/mainpage it's redirect to www.page.com/mainpage.php and not to www.page.com/index.php?arg1=mainpage
my .htaccess looks like
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?arg1=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)?/?$ ?arg1=$1&arg2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?/?$ ?arg1=$1&arg2=$2&arg3=$3 [L,QSA]
You probably have the MultiViews enabled. Also, your rewrites are not referencing index.php anywhere. Try the following:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?arg1=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)?/?$ index.php?arg1=$1&arg2=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?/?$ index.php?arg1=$1&arg2=$2&arg3=$3 [L,QSA]

.htaccess RewriteRule country codes as a get param

What I want to achieve:
http://example.com/en -> http://example.com/?lang=en
http://example.com/en/something -> http://example.com/something/?lang=en
http://example.com/ru -> http://example.com/?lang=ru
http://example.com/ru/something -> http://example.com/something/?lang=ru
I try with:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ /?p=$1&lang=en [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ /?p=$1&lang=ru [L,QSA]
You can do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/(.+)$ /$2/?lang=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/?$ /?lang=$1 [L,QSA]
(ru|en) will match either ru or en. If you wanted to make it a generic 2 letter match, you could use [a-z]{2}

how to escape "/" from mod_rewrite

this is my .htaccess file thnx to #Gyrocode-com
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/(.*)$ fun.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/(.*)$ user.php?u=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L] #my problem is here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([1-9]+)$ index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ index.php?c=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /jokes/$1/ [R]
everything works great except whene i try to go to this link
localhost/u/user1/2
which suppose to take me to page number 2 of user1
but instead it catches user1/2 as the value of variable (u)
and what I want is u=user1 ; p= 2;
Your rules can be refactored to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^p/([^/]+)/?$ fun.php?p=$1 [L]
RewriteRule ^u/([^/]+)/?$ user.php?u=$1 [L]
RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L]
RewriteRule ^([1-9]+)$ index.php?p=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]
RewriteRule ^([^/]+)/$ index.php?c=$1 [L]
RewriteRule ^([^/]+)$ /jokes/$1/ [R,L]
Using ([^/]+) instead of (.*) to restrict matching to anything before /
Instead of using RewriteCond %{REQUEST_FILENAME} -f and RewriteCond %{REQUEST_FILENAME} -d before each rule it is better to use that in a separate rule as shown.

htaccess rewrite 2 rule Simultaneously

I hava an htaccess file.it pass url to index.php
<!-- language-all: lang-html -->
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
now i wanna to remove "www" befor my domain name and also keep my previous RewriteRule .
How to do it؟
thanks
You can have a new rule for www removel before your current rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]