Removing Trailing Slash with .htaccess - mod-rewrite

I am trying yo remove the trail slash from an url with this pattern
http://localhost/~francesco/mycms/about/
to make it
http://localhost/~francesco/mycms/about
I have tried lots of rules but no one is working for me.
My rewrite rule is this by now
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~francesco/mycms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
</IfModule>
Hope someone can help me!

Try placing this rule
RewriteRule ^(about)/$ $1 [L,R=301,NC]
just after
RewriteBase /~francesco/mycms/
If you want to work for any first level directory following the RewriteBase i.e. http://localhost/~francesco/mycms/[anything-here]/ use
#exclude the /~francesco/mycms/admin/directory
RewriteCond %{REQUEST_URI} !^/admin/ [NC]
RewriteRule ^([^/]+)/$ $1 [L,R=301,NC]

Related

Redirect user using htaccess in my Laravel application

I am having trouble with my configuration of htaccess in my laravel application.
I am using the following codes to redirect user to the right path. I tried to add host_name to my htaccess but it breaks the inner pages of the application.
IP address redirection is working fine now.
File : laravel_app/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^10\.10\.10\.201$
# RewriteCond %{HTTP_HOST} ^host_name$ -- It breaks the other page.
RewriteRule (.*) http://host_name.maindomain.com/$1 [R=301,L]
</IfModule>
User can manually navigate to the ff:
http://host_name/login
http://10.10.13.201/login
But i want them to use the rigth path below :
http://host_name.maindomain.com/login
Is this achievable using htaccess?
Edit :
http://host_name.maindomain.com is pointed to laravel_app/public.
You can not chain the RewriteCond, so your .htaccess has an invalid configuration since it will only try to bind the last condition and the first one has no corresponding rule:
Try something like this:
RewriteCond %{HTTP_HOST} ^10\.10\.13\.201$
RewriteRule (.*) http://host_name.maindomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^host_name$
RewriteRule (.*) http://host_name.maindomain.com/$1 [R=301,L]
See the demo

Ajax crawable content .htaccess rewrite rule

I've been trying to figure this one out the past couple of hours but can't get it to work.
I'm tyring to send google to a server side generated version of each page of my AngularJS app.
My angular URLs look like the following:
http://localhost:8000/#!/product/123/product+name
The static versions generated have the following URL stucture:
http://localhost:8000/product/123/product+name
So they are both quite similar. I've tried a few different rewrite rule configurations but none have worked so far. For example:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /%1? [NC,L]
Any help would be greatly appreciated. Thanks!
EDIT:
I forgot to say Google and other search engines convert the hash bang URLs into URLs like
http://localhost:8000/?_escaped_fragment_=/product/123/product+name
Current .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/$ [NC]
RewriteRule ^ /snapshots/index.html? [NC,L]
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /%1? [NC,PT]
</IfModule>
I think i found the problem.
Try using PT flag instead of L flag because of your internal redirect on symlink.
This way, it is re-evaluated
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /%1? [NC,PT]
EDIT: You have to reorder your rules. Your htaccess should look like this
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /%1? [NC,PT]
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Apache2 mod_rewrite - how to add trailing slash to only one subdirectory

I'm using Apache 2.2 with two servers:
Development: localhost/project/public
Production: www.example.com
I have an existing rewrite rule for clean urls (to remove 'index.php' from the url).
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have one special route that only works if a trailing slash is present:
special route (development): localhost/public/documentation/
special route (production): www.example.com/documentation/
How do I add a rewrite rule to my existing .htaccess to always add a trailing slash, but only for the documentation route?
You just need to add another RewriteRule that matches only against /documentation i.e. without a trailing slash. The first rule adds the trailing slash and then your existing rule adds the index.php.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(documentation)$ $1/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you would like the browser's address bar to also reflect the trailing slash change the rule to
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(documentation)$ $1/ [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Don't forget to add these directives first once per .htaccess file:
Options +FollowSymlinks
RewriteEngine on
And then you can try this:
RewriteRule ^/?(documentation)$ /$1/ [R,NC,L]
Or this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} (.+)/([^/]+)$
RewriteRule ^/?(documentation)/(.+)$ /$1/$2/ [R,NC,L]
I'm confused about your question..

How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?

For the past 3 days I have been playing around with Apache's mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.
Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2
While the user needs to see this
http://example.com/Page/Param1/Param2
What I have right now is the following in an htaccess file
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]
Which was taken from another page, and is close to what I need. However this seems to be cutting off everything after the http://example.com/ part. How can I get mod_rewrite to show the user one thing and have php see something else?
This is the modified code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
Change R=302 to R=301 once you're satisfied that it is working fine for you.
This rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Needs to look like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ /%1 [R=301,L]
Also note that RewriteRule ^(.*)$ index.php?$1 [L,QSA] doesn't create a URI that looks like this /index.php/Page/Param1/Param2, it creates a query string that looks like this: /index.php?Page/Param1/Param2. Which isn't at all what you said PHP needs to see.

Question about url rewriting using apache mod_rewrite

I have a doubt about url rewriting using apache mod_rewrite. I am a newbie in mod_rewrite and I don't have any experience in regex.
What I want to do is to:
Rewrite / To /web/content/public/
Rewrite /clients/ To /web/content/clients/
How can I achieve above things.
I tried:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^(.*)$ web/content/public/$1 [L]
</IfModule>
But it doesn't work. What can I do?
^(.*)$ includes the slash. So don't include the slash in the rewritten pattern.
But include a root slash at the head of your rewritten pattern.
RewriteRule ^/clients(.*)$ /web/content/clients$1 [L]
RewriteRule ^(.*)$ /web/content/public$1 [L]
Check the apache access log and error log to see what kind of request URL comes back.
Please check the documentation, especially the list labeled "Here are all possible substitution combinations and their meanings:"
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^/(.*)$ web/content/public/$1 [L]
And if you want to use that rules in a .htaccess file, remove the leading slash from the patterns.

Resources