force htaccess to rewrite url from last slash - .htaccess

I need to use htaccess to change below url:
http://example.com/main/en/index.php?page=pages&page_id=9
to:
http://example.com/main/en/pages/9.html
I've done it by the below rule:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ /main/fa/?index.php?page=$1&page_id=$2 [L]
but there is a problem, this rule make my display url to:
http://example.com/pages/9.html
I want to htaccess change url just after last slash.
Because I must have more than one language, it will have conflict with other languages. How must I write this rule?

Maybe this is what you are looking for:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([^/]+)/([^/]+)/([\d]+)\.html/? [NC]
RewriteRule .* main/%1/index.php?page=%2&page_id=%3 [L]
Will redirect internally:
http://example.com/main/LangCode/PageName/PageID.html
To:
http://example.com/main/LangCode/index.php?page=PageName&page_id=PageID

Related

htaccess redirection conflict

I am writing code in htaccess to add trailing slashes in url, also applying redirection.
My code is-
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Redirect 301 /social-media-consultant http://example.com/seo-consultant/
Redirect 301 /uiux-developers http://example.com/graphics-designer/
Now, problem is that when I write code for adding slashes then redirection stops working.
Change order of your rules
Use only mod_rewrite based rules.
Have it this way:
RewriteEngine On
RewriteRule ^social-media-consultant$ http://example.com/seo-consultant/ [L,NC,NE,R=301]
RewriteRule ^uiux-developers$ http://example.com/graphics-designer/ [L,NC,NE,R=301]
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^ http://example.com/%{REQUEST_URI}/ [L,R=301,NE]
Make sure to clear your browser cache before testing this change.

htaccess optional parameter

I have a very simple rewrite rule. I would like the second parameter in the URL to be optional, but as it stands at the moment i have to pass it in the URL:
RewriteEngine On
RewriteRule ^signup/([^/]*)/([^/]*)/$ /signup/index.php?e=$1&s=$2 [L]
I have the URL /signup/PARAMETER/OPTIONAL-PARAMETER/
How can I make the url work if I leave the second parameter off?
Joe
Just add a single parameter rewrite after it:
RewriteEngine On
RewriteRule ^signup/([^/]*)/([^/]*)/$ /signup/index.php?e=$1&s=$2 [L]
# single parameter
RewriteRule ^signup/([^/]*)/$ /signup/index.php?e=$1 [L]
I used the answer from this question htacess and two post parameters .... Thanks aeshabana for the tip!
I also needed to add a slash to the URLS so here is my final .htaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^signup/(.+)/$ /signup/index.php?uri=$1 [QSA,L]

htaccess - URL rewrite - Remove slashes, but not from files

I use PHP.
A working htaccess-file
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php/$1 [L]
Look at the row with a # comment. When uncommented it adds a redirect to a slash. I use URL rewrite with toroPHP.
I want to rewrite to ending slash
I want to redirect to ending slash from rewritten URLs, just like the code above.
I don't want ending slash from real files, like jquery.js, style.css.
Example (updated 2012-12-21)
/category/test should be /category/test/
http://www.test.com/myjsfile.js should be http://www.test.com/myjsfile.js
Problem
If I use the code above uncommented it add an ending slash to all urls, including javascript files and css files.
I only want the rewritten urls to end with slash.
Question
Can it be done with htaccess? If so how?
The htaccess was almost correct. However the REQUEST_FILENAME needs to be before EVERY rewrite rule, not just before the first.
This works
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*[^/]$ $0/ [L,R=301]
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]

How to change URL? htaccess

What I have
newsy/czytaj/items/odbierz-250zl-na-reklame.html
This is what I would have
newsy/odbierz-250zl-na-reklame.html
How to do this with mod-rewrite? I don't understand RewriteRule.
My .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} (ftp|https?):|/etc/ [NC,OR]
RewriteCond %{QUERY_STRING} (ftp|https?):|/etc/ [NC]
RewriteRule .* - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*\.html$ index.php [L]
RewriteEngine on
RewriteRule ^newsy/([^\./]+)\.html /newsy/czytaj/items/$1.html [L]
This will rewrite anything that starts with newsy and add a /czytaj/items between it and the html file.
In principle you just create a corresponding rewrite rule:
RewriteRule ^newsy/czytaj/items/(.+) /newsy/$1 [L]
It is crucial not to omit [L]flag. Otherwise your rewrite engine may get stuck in an endless loop. Also in the beginning of the .htaccessfile remember to enable mod_rewrite with:
RewriteEngine On
For more help on mod_rewrite, I recommend checking out mod_rewrite-cheatsheet. For an exhaustive URl Rewriting Guide see a corresponding page from Apache 2.0 Documentation.

htaccess server side rewrite not working

I have my htacess rewrite working, the pages are going to where they are supposed to, but the url bar changes and I dont want it to. I thought this was an INTERNAL redirect and whatever is in the URL would be displayed. It's not working that way.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))?$
RewriteRule . /%2 [L]
Basically, the url IS rewriting to the new URL. How do I get it to not do that?
Per your last comment, try this instead
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/?|/(.+))$
RewriteRule . /%2 [L]
It should match an optional trailing slash e.g. http://mydomain.com/somedir or http://mydomain.com/somedir/ and one with a directory after e.g http://mydomain.com/somedir/dir2
If whatever the %2 back reference is matching doesn't end with an extension, mod_dir might think that it's a directory. If it's missing a trailing slash, mod_dir will externally redirect the browser to the same URL but with a trailing slash. You could try turning DirectorySlash Off in your .htaccess file or server config.
edit
You can try to bypass mod_dir's by doing the directory check yourself and adding the trailing slash so mod_dir won't redirect you. It would look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))$
RewriteCond %{DOCUMENT_ROOT}/%2 -d
RewriteRule ^[^/]+(/(.+))$ /$2/ [L]

Resources