remove index.php and foldername with mod_rewrite - .htaccess

The URL Structure on my website is the following: http://www.mydomain.tld/index.php/pages/sitename. Now I want to remove the index.php/pages, to let the URL Structure be like http://www.mydomain.tld/sitename. Furthermore, I want to rename some sitename's individual.
I already removed the index.php, thats no problem. So currently my .htacces-File looks like
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico|pam)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^mydomain.tld\.de$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
But if I append /pages to the first RewriteCond or RewriteRule, it doesn't work.
Do you have some ideas?

Have your .htaccess like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.tld\.de$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!index\.php/pages/).*)$ index.php/pages/$1 [NC,L]

Related

remove index.php from url only for the client

I know there are some questions like this out there but the solutions are not helpful for me.
I´m using htaccess to redirect to the index.php if the request file isn´t defined (code above).
I´d like to add a code-line that if the user requests for example:
http://www.example.com/test/index.php
he will redirect to
http://www.example.com/test/
but internal it will redirect to the index.php.
Is there a way to extend my code so that it works like I want it to?
# Add Slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# Redirect to index.php if the following sites were not match
RewriteBase /
RewriteCond %{REQUEST_URI} \.(php|sql|tgz|gz|txt|ttf|TTF|log|txt|ini|html|xml|xhtml|htm)$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/phpinfo.php$
RewriteCond %{REQUEST_URI} !^/out.php$
RewriteCond %{REQUEST_URI} !^/sitemap[^0-9]*\.xml$
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
Complete code:
DirectoryIndex index.php index.html
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Take off index.php
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.(php|sql|tgz|gz|txt|ttf|TTF|log|txt|ini|html|xml|xhtml|htm)$
RewriteCond %{REQUEST_URI} !^/((index|phpinfo|out)\.php|robots\.txt|sitemap[^0-9]*\.xml)$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?dir=$1 [QSA,L]
# Add Slash for non-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

Mod rewrite remove trailing slash but keep on directories

So far my .htaccess is like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<FilesMatch "\.(htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$">
RewriteEngine Off
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*\.html$ index.php [L]
I would like to have url with trailing slash redirected to the one without. But the slash for directories should be kept.
Any help would be very appreciated!
UPDATE:
after some more Google research i assembled some working solution for me. Both rewriting for removing trailing slash and redirect for www are working.
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^rudolfapotheke.de$ [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
First, I would change your FilesMatch to a RewriteCond
RewriteCond %{REQUEST_FILENAME} !\.(?:htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and simplify the pattern for the next RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.html$ index.php [L]
or put the two rules together and test just for
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and finally the rule for removing the trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
To redirect those with trailing slash, you need to check if the path is a directory with a RewriteCond and then include a RewriteRule if the filename matchs /$
So it would look something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1 [R=permanent,L]

.htaccess single variable url rewrite

I am trying to redirect http://test.pearlsquirrel.com/explore_all?u=hiphop to http://test.pearlsquirrel.com/explore_all/hiphop using htaccess. However, It keeps redirecting here: http://test.pearlsquirrel.com/explore_all/?u=site_error.php. I have looked all over Stackoverflow for the answer but I just can't seem to find it. I would really appreciate some help in fixing this problem, thanks!
Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pearlsquirrel.com$ [NC]
RewriteRule ^(.*)$ http://pearlsquirrel.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) explore_all?u=$1 [L]
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} [^=]+=([^=]+)
RewriteRule .* /%1? [L]
Will map this:
http://test.pearlsquirrel.com/explore_all?u=hiphop
To this:
http://test.pearlsquirrel.com/explore_all/hiphop
Replace the corresponding code in your .htaccess file with this one.
I have not checked the rules in your .htaccess file.
OPTION
Now, if it is the other way around, which I think it is, and the idea is to map this incoming URL:
http://test.pearlsquirrel.com/explore_all/hiphop
To this resource:
http://test.pearlsquirrel.com/explore_all?u=hiphop
Replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([^/]+)/?
RewriteRule .* http://test.pearlsquirrel.com/explore_all?u=%1 [L]

don't rewrite static css/js/img files

I'm trying to get my htaccess file not to rewrite my static files (js/css/images).
This is my current htaccess file:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(jpg|css|js|gif|png)$ public/ [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1
How do I rewrite it?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ public/index.php?url=$1
All requests to not existing files which doesn't end with listed extensions (case nonsensitive match) are rewritten to public/index.php passing the current URL as url= GET argument
This only works for the one line below it. So if you have many RewriteRules it could help to use something like this:
RewriteEngine on
RewriteRule ^(.*?)\.(php|css|js|jpg|jpeg|png|pdf)$ - [L]
RewriteRule ^(.+)/(.+)/?$ index.php?page=$1&subpage=$2 [L]
RewriteRule ^(.+)$ index.php?page=$1 [L]
for posterity.
Maybe my rules will be useful
\www\.htaccess
#file: \www\.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1?a [QSA,L]
RewriteCond %{REQUEST_URI} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?) [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1 [QSA,L]
#RUN TEST
RewriteCond %{REQUEST_URI} !\.(css|js|html?|png|gif|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule ^(.*)$ index.php?REQUEST_FILENAME=%{REQUEST_FILENAME}&date=$1&REQUEST_URI=%{REQUEST_URI} [QSA,L]
#END TEST
RewriteRule . index.php?url=%{REQUEST_URI} [QSA,L]
ErrorDocument 404 index.php?error=404
\www\index.php
<?php
// file: /www/index.php
var_dump($_GET);
\www\app\.htaccess
#file: \www\app\.htaccess`
RewriteEngine On
RewriteRule ^(.*)$ ../_%{REQUEST_URI} [QSA,L]
\www\public\.htaccess
#file: \www\public\.htacces`
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=%{REQUEST_URI}&rewrite=$1 [QSA,L]
/www/public/index.php
<?php
// file: /www/public/index.php
var_dump($_GET, __DIR__);

htaccess redirect from subdirectory to subdirectory

I'm trying to set up a local instance of a php site working in a server, in the "/" directory. My local instance will work in my development environment in a "/subdirectory/".
I'm trying to translate .htaccess to adapt to this, but I always get a 404. This is original .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^my-nice-url$ /index.php?p=ugly_url
And these are my tries:
RewriteEngine on
RewriteBase /mylocaldirectory/
# Not in local! RewriteCond %{HTTP_HOST} ^example.com$ [NC]
# Not in local! RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^my-nice-url$ /index.php?p=ugly_url
Also
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)my-nice-url$ /index.php?p=ugly_url
Thank you
If i got you right, you want make a user friendly url, i do it like this:
RewriteRule ^(.*)/(your-nice-url)$ ($1)/$2/ [R=301]
RewriteRule ^(.*)/(your-nice-url)/$ $1/index.php?p=$2 [L]
or
RewriteRule ^(.*)/(your-nice-url)$ ($1)/$2/ [R=301]
RewriteRule ^(.*)/(your-nice-url)/$ $1/index.php?p=what-ever-you-need [L]
Hope it help you.

Resources