how to escape "/" from mod_rewrite - apache

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.

Related

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]

Remove parameters from url using .htaccess

I want to remove parameters from url,
I need change this:
http://example.com/?page=about
To this:
http://example.com/about
How?
This is my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Try the following in htaccess :
RewriteEngine On
#Remove php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]
#rewrite /about to /?page=about
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /?page=$1 [NC,L]
this will rewrite
/about
to
/?page=about
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [QSA,L]

I want to be able to search in two sub-directories with .htaccess

I want to be able to search in two sub-directories with .htaccess
I have the following directory structure
My problem is:
When for example a request on
resources/images/players_small_pigs/BMI10.png
is being received, I would like to be able to search in the path
webapp/public/resources/images/players_small_pigs/BMI10.png
is the file not to find here, I would like to continue the search in
webapp/adminpanel/resources/images/players_small_pigs/BMI10.png
Can some one help me to get it to work by using the .htaccess file? The problem throughout the entire folder structure resources, views, and webservices, I think it might be a bad structure I have and maybe I should put them all in a folder, rather than having it split up between a adminpanel and public folder, what would you recommend?
I have the following code in .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(resources/.*)$ /webapp/public/$1 [L]
RewriteRule ^(resources/.*)$ /webapp/adminpanel/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/public/webservices/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/adminpanel/webservices/$1 [L]
RewriteRule ^(public/resources/images/)(sponsors/)?(.*)$ /webapp/$1$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} adminpanel
RewriteRule (.*) /webapp/adminpanel/views/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !adminpanel
RewriteRule (.*) /webapp/public/views/$1 [L]
RewriteRule ^$ /webapp/public/views/index.php [L]
You can give the following rule a try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} [L]

htaccess - how to capture the current rewrited url?

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]

RewriteCond of htaccess is not working properly

Below is my .htaccess file:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
I have some issues:
lib and applications are folders, but RewriteCond %{REQUEST_FILENAME} !-d has no effect, I have to write condition for lib and application separately.
The main issue is my api calls are not giving me result. I am calling my api as www.myweb.com/api/User/xyz%40gmail.com which is redirecting to www.myweb.com/api/index.php/api/User/xyz%40gmail.com, which is right. But calls to this are blank.
A RewriteCond only gets applied to the immediately following RewriteRule. Conditions aren't set globally nor get applied to more than one rule. You'll either need to repeat the condition for each rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
Or you can negate the condition and let it pass through first:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
This way, the opposite of your conditions get applied to a rule that simply says "do nothing, and no more rewriting". In order for any of the bottom 5 rules to get applied, the conditions at the top would not have been met.