I have this rewrite condition
I use it to grab the slug url and search for a blog post like this:
http://localhost:8080/blog/my-first-post
So I grab the slug and then look for the post and display it.
Right now it accepts this two forms
http://localhost:8080/blog/my-first-post
http://localhost:8080/blog/my-first-post/
How should I change the .htaccess so it always adds or redirects with the slash at the end.
And how should I change it to remove the slash always?
Here is the .htaccess I have now which I have it in the blog directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
You can replace your current htaccess code by this one
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
# add trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1/ [R=301,L]
# remove trailing slash
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+?)/$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Right now, it adds trailing slash. If you want to remove it, comment add trailing slash block lines and uncomment remove trailing slash block lines
Related
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
I am having some problems with using .htaccess to rewrite three filenames.
Basically the url: domain.com/post.php?category=uncategorized&title=title-of-post&id=1
should be rewritten as
domain.com/uncategorized/title-of-post-1
I worked out how to just rewrite one of the filenames (such as the categorized part) but how do I do all three, so that the category will be in one directory, and the second directory will be a merge of the title and id separated by a dash?
This is my code so far:
Options +FollowSymlinks
RewriteEngine on
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w\d~%.:-]+)/?$ category.php?id=$1 [L,QSA]
I also need the URL to end with a slash. If it is not, it will redirect to it.
You can insert a new rule for post.php:
Options +FollowSymlinks
RewriteEngine on
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/(.+?)-([^/-]+)/?$ post.php?category=$1&title=$2&id=$3 [L,QSA]
RewriteRule ^([^/]+)/?$ category.php?id=$1 [L,QSA]
I tried to get rid of "index.php" in url using the following rewrite code.but it's not working
please help me to fix this bug
# Development
RewriteEngine On
RewriteBase /mvcTestApp/blog/ciBlog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
In .htaccess file, add the following.
#Rewrite index.php
#Start using rewrite engine
RewriteEngine On
#Rewrite condition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Whenever index.php is there in the url, it will rewrite to / automatically
RewriteRule .* index.php/$0 [PT,L]
Check this link for more detail: http://subhra.me/remove-index-php-urls-codeigniter/
This htaccess is working for me.Try this.
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
Use this rule
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
missing "/" before "index.php" in last rule
or refer URL
http://ellislab.com/codeigniter/user-guide/general/urls.html
Here's my suite of rewrite rules for CodeIgniter. Please read the inline comments:
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Remove the "index.php" part of URI
# Remove access to "codeigniter" and "data" folders
RewriteCond %{REQUEST_URI} ^(codeigniter|data).*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Block access to "hidden" directories whose names begin with
# a period. e.g. .git, .svn
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
# WWW redirect
RewriteCond %{HTTP_HOST} ^(?!www\.).*
RewriteCond %{HTTP_HOST} ^(?!dev\.).*
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the root index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
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]
I am using .htaccess to manage clean URL's but for some reason I get a 404 Not Found Apache error in some cases.
If a user goes to http://domain.com/profile/ everything is fine.
If a user goes to http://domain.com/profile they will get the error
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&cmd=$2 [L]
Try this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /profile/cmd
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&cmd=$2 [L]
# /profile
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
</IfModule>
You can redirect the url with the url which has trailing slash using 301 redirect
Try the following along with your rewrite condition. This will redirect any url request to url with trailing slash. ex. http://domain.com/profile will be redirected to http://domain.com/profile/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
The rewrite rule you wrote requires one slash '/'.
Try this instead:
RewriteRule ^([^/]*)(/([^/]*))?$ /index.php?page=$1&cmd=$3 [L]