Stop URLRewrite check after slash - apache

Below is my current Apache configuration in .htaccess file
RewriteRule ^(user)($|/) - [L]
RewriteRule ^user/([^/]+) /user_/index.php?u=$1 [L]
I want to rewrite www.mydomain.com/user/test to www.mydomain.com/user_/index.php?u=test
But, I don't want to rewrite anything if it is only www.mydomain.com/user or www.mydomain.com/user/ where user is a directory in my server.
How can I do that?

Try this:
AddDefaultCharset UTF-8
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /?user/(.*)$
RewriteRule /?user/(.*)$ user_/index.php?u=$1 [QSA,L]
here:
RewriteCond %{REQUEST_URI} /?user/(.*)$ for check condition

You need to use quantifier .+ to make sure you don't match /user or /user/ like this:
RewriteEngine on
RewriteBase /
RewriteRule ^user/(.+)$ user_/index.php?u=$1 [QSA,L,NC]

Related

Web site link change using HTAaccess

Is it possible to use .htaccess to rewrite a sub domain?
Example:
http://fashion.example.com
shows the content of
http://example.com/index.php?name=fashion
<IfModule mod_rewrite.c>
# Turn on the rewrite engine
RewriteEngine on
RewriteRule %{REQUEST_URI} ^(.*).example.com/$ index.php?name=$1
</IfModule>
You may try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.com$ [NC]
RewriteRule ^ index.php?name=%1 [L,QSA]
``
Assuming your subdomain is pointing to the same directory as your main domain, you can use something like the following :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^((?!www).+)\.example.com$
RewriteRule ^(.+)/?$ /index.php?name=%1 [L]

Remove or add trailing slash from CakePHP 2.x on Apache using .htaccess

I am trying to enforce the removal or add of trailing slashes to my CakePHP 2.x app using the following inside the .htaccess located at /app/webroot.
#<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^ index.php [L]
#</IfModule>
<IfModule mod_rewrite.c>
# Add trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|)$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
</IfModule>
or the following for add the trailing slash.
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
But I don't want to enforce the domain as it could be running locally or inside a subfolder. How do I fix this? All the docs I've read online seem to enforce the FULL url of the app.
In fact even if I use the FULL url it still doesn't work. Has anyone managed to get this working with CakePHP? Seems you MUST run it through the index.php file
To clarify I'm looking for a solution for ADDING and REMOVING trailing slashes in CakePHP without having to hardcode the url into the .htaccess file.
This is usually not a complicated thing.
What about this ?
<IfModule mod_rewrite.c>
RewriteEngine On
# if this is an existing folder/file then leave
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
# if trailing slash then redirect to url without trailing slash
RewriteRule ^/?(.+)/$ /$1 [R=301,L]
# internal rewrite to controller/dispatcher index.php
RewriteRule ^.*$ /index.php [L,QSA]
</IfModule>
If this one is not working, well this could be a problem on your side.
Did you already use mod_rewrite before ? Is it enabled ?
EDIT: if your website is not in root folder (DOCUMENT_ROOT) you have 2 options. You could put your htaccess in application folder or in root folder.
I suggest you to put your htaccess in your application folder (dedicated for your website only). Otherwise, the code would not be exactly the same. Here's the code in this case:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /application/
# if this is an existing folder/file then leave
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
# if trailing slash then redirect to url without trailing slash
RewriteRule ^/?(.+)/$ $1 [R=301,L]
# internal rewrite to controller/dispatcher index.php
RewriteRule ^.*$ index.php [L,QSA]
</IfModule>
Your second question:
Also what would be the code for ENFORCING a trailing slash?
You could handle that with this code (only one line changed)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /application/
# if this is an existing folder/file then leave
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
# if no trailing slash then redirect to url with trailing slash
RewriteRule ^/?(.+)([^/])$ $1$2/ [R=301,L]
# internal rewrite to controller/dispatcher index.php
RewriteRule ^.*$ index.php [L,QSA]
</IfModule>
Try this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
</IfModule>
Try this (bakery):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
#RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
If you want to do it for SEO you always have an option to use canonical links and stop bother about redirects:
<link rel="canonical" href="/your_controller/action/"/>

Issue with URL rewriting and parameter GET

I have some problem to build my url rewrite. On my local server, working with nginx, everything is going well. But as soon as I try to make it working for apache with htacces, I kind of get crazy.
My urls are looking like this:
http://example.com/controller/action/param1/value1/param2/value2
Following is the htaccess working for the previous case:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(images|css|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
But in some case I have some extra parameter, for example for Facebook authentication:
http://example.com/controller/action?hauth.start=Facebook&hauth.time=1382665465
This case is not working with the previous htaccess. Nothing is going in the GET variables. How could I fix this?
Use QSA (Query String Append) flag in your rule
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(images|css|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
</IfModule>
QSA flag makes sure to preserve an existing query string while adding new query parameters.

mod_rewrite not working for query string redirect

Currently I have one rewrite rule for pretty urls, which is working fine. I tried adding in a second, and it's giving me problems:
# redirect /?p=xyz to /xyz
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ /index.php/%1 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
The second one is working fine, but the first one is giving me internal server error. Basically I want anything in ?p=* to go to /index.php/*
[Edit] Just to clarify, I need http://domain.com/?p=test to be equivalent to http://domain.com/test/
domain.com/test to domain.com/?p=test
Try this instead:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !p= [NC]
RewriteRule ^([^/]+)/? /?p=$1 [L,NC]
OPTION:
In case it is the opposite:
domain.com/?p=test to domain.com/test/index.php
Try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} p=([^/]+) [NC]
RewriteRule .* /%1/index.php? [L,NC]
Remove index.php in case it is not needed, like this:
RewriteRule .* /%1/? [L,NC]

.htaccess redirect and add variables

our clients banner campain lauched today an the wrong url has been deposited.
So i try to redirect
website.com/wheels/?lang=en&sel=540
to
website.com/wheels/index.html?lang=en&sel=540
I tryed to put a .htaccess in the folder wheels which contains following:
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/?$ index.html?lang=$1&sel=$2
But it doesn't work. Anyone has an idea, what i'm doing wrong?
EDIT
I found a existing .htaccess in the ROOT. It contains following code:
RewriteEngine On
RewriteBase /
RewriteRule ^(typo3|typo3temp|typo3conf|t3lib|tslib|fileadmin|uploads|showpic\.php)/ - [L]
RewriteRule ^typo3$ typo3/index_re.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php [L]
If I add the code folling code, nothing happens:
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
Btw, lang is not static, it can be en or de.
Try adding this to the .htaccess in the root directory of your website. The querystring parameters are carried through by default.
RewriteEngine on
RewriteBase /
RewriteRule ^wheels/$ /wheels/index.html [R=301,L,NC]
Use this in your ROOT .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=en&sel=[0-9]+(&|$) [NC]
RewriteRule ^(wheels)/$ /$1/index.html [R=301,L,NC]
You could also do this by adding an extra RewriteCond in front of the existing RewriteRule
RewriteCond $0 !^wheels/