I'm trying to have only one URL without the trailing slash at the end of the URLs/folders, but at the same time I want to re-write this folder to use dynamic pages, for example:
if I have:
mydomain.com/folder
mydomain.com/folder/
and
mydomain.com/folder/second
mydomain.com/folder/second/
then I want to open only "mydomain.com/folder" and "mydomain.com/folder/second" and this URLs should open a dynamic pages. This is what I am trying, but it does not work, it crash my page:
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L]
RewriteRule ^(.*)$ includes/category.php?url=$1&pageType=category [L]
RewriteRule ^(.*)/(.*)/$ /$1/$2 [L]
RewriteRule ^(.*)/(.*)$ includes/subcategory.php?url=$1&pageType=subCategory [L]
What am I doing wrong? can you help me to run the right syntax please?
You need to redirect the trailing slash URL's instead of just rewriting them. You also probably need to add some conditions to the rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /includes/category.php?url=$1&pageType=category [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /includes/category.php?url=$1&pageType=subCategory [L]
RewriteEngine On
RewriteRule ^([^\/]+)/?$ includes/category.php?url=$1&pageType=category [L]
RewriteRule ^([^\/]+)/([^\/]+)/?$ includes/subcategory.php?url=$1&pageType=subCategory [L]
you may also add the following conditions (before rewrites) to exclude real file and folders from those rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Related
I have the following .htaccessfile
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And I have the following url:
http://localhost/mysite/loginPage/
I want to ensure that this url will never ends with forward slash (loginPage/).
I tried to use the following lines:
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^([^/]+/[0-9]+/[0-9]+/[0-9]+/[^/]+)/.+$ - [L,R=404]
But I get error message in all cases that page is not redirecting properly. Please have in mind that I want this just for the specific URL above, i don't care for the rest of the site.
It depends on where you put the rule for your rewrite. The examples you provided do work. But you have to put them before your last htaccess line.
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
This will create the proper output, and can be tested through something like a htaccess tester
If you want it specifically for just that url you could resort to:
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/loginPage/$ mysite/loginPage? [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
You could also specify an absolute redirect with
RewriteRule ^mysite/loginPage/$ http://%{HTTP_HOST}/mysite/loginPage? [R=301,L]
I want this just for the specific URL above
In that case, your rule should just match specifically this URI rather than .*/.* etc.
Have your rules like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/mysite/loginPage)/$ [NC]
RewriteRule ^ %1 [L,R=301]
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php/$0 [L]
You don't really need QSA flag in last rule also.
Make sure to clear your browser cache before testing this change.
Can't figure out what I'm doing wrong.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)\.php$ detail.php?name=$1 [NC,QSA,L]
RewriteRule ^(.+)/directory/\.php$ detail2.php?name=$1 [NC,QSA,L]
The first RewriteRule should redirect anything (ending on php like domain.com/product1.php) from the root domain to detail.php (it can not affect things like domain.com/contact.php)
A 2nd RewriteRule should redirect anything from domain.com/directory/product-b1.php to detail2.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /foldernamehere/index.php/$0 [PT,L]
paste this code in your .htaccess file it will resolve your problem
I am doing a project with fetching data from database. I am used URL rewriting method. I was using rewrite method it will redirect to error page
Dynamic url
http://www.sample.com/?cat=kk
The rewritten URL
http:/www.sample.com/kk
the .htaccess file written for this URL rewrite
RewriteEngine On
RewriteRule ^([^/]*)$ /?cat=$1 [L]
The .htaccess file also contain another url rewrite
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&year=$3&page=$4 [L]
RewriteRule ^([^/]*)/([^/]*)$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /?cat=$1&sub=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&pag=$3 [L]
RewriteRule ^([^/]*)/$ /?cat=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The problem I am having is that when I access any page, it will redirect to server default error page.
I'm sorry to say that, but your rewriterules are messy.
Always start from the most complex to the simplest one.
And you've forgotten one of the most important directives: QSA. I let you google for this ;)
Maybe what follows still doesn't work, but it's cleaner.
We need log files to know what's happening.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&year=$3&page=$4 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /?cat=$1&sub=$2&page=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /?cat=$1&sub=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /?cat=$1 [QSA,L]
I'm having issues with my mod_rewrite module here. If I do /toronto/ it will direct me accordingly, however, if I do /toronto without the trailing slash it comes back with a 404. I need /toronto and /toronto/ to both read from the /city_name folder. How can I avoid having the trailing slash, here's my code:
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /city_name/index.php?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L,QSA]
RewriteRule (.*)/$ /city_name/index.php?page=$1 [L,QSA]
This rule explicitly says that the URI has to end with a slash, so 'toronto' matches no rules. To make the slash optional, use the ? operator:
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]
this is my scenario. My application urls are showed like this:
example.com/index.php/article/whatever/here
in order to remove the index.php from url I have put this in my .htaccess and works fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my problem is that I have include and admin.php like this:
example.com/admin.php/manage/whatever/here
And I don't know how to remove the admin.php from url without make conflics with the index.php because I have tried this and a 500 Internal Server Error is showed:
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /admin.php/$1 [L]
EDIT
Another try was this and nothing, because a 404 error is showed:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
ALL my urls should have several parameteres as requiered:
example.com/this/is/a/large/url/because/is/requiered
EDIT 2:
I have create two rules, each rule alone works fine!!! but both can't work together:
RewriteRule ^admin/(.*)$ /admin.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} ^admin/(.*)$ [NC]
RewriteRule ^(.*)$ /admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then at least test this with:
http://www.example.com/some/url
http://www.example.com/admin/some/url
You can slightly modify your rewrite rule to capture the first part of the path, and use it in the replacement. Try this:
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
Basically the () create a capturing group, so anything between the first two / will be captured as $1 and anything after that will be captured as $2
Note that you can also change your rewrite conditions, if you only want this replacement to apply to index.php and admin.php