I'm using HTACCESS in my website but I'm facing a small problem with one of the files/links.
My link is http://localhost/photos/view/1465574353
My HTACCESS is:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?category=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1¤tpage=$2 [NC,L,QSA]
RewriteRule ^view/([0-9]+)/?$ view.php?id=$1 [NC,L,QSA]
When I follow the link I am not redirected anywhere at all, yet when I follow http://localhost/photos/view.php?id=1465574353 I am shown the page.
Anyone know the reason why this may be?
** If I change it to
RewriteRule ^view/([0-9]+)/([\w-]+)/?$ view.php?id=$1&title=$2 [NC,L,QSA]` and visit my page at `localhost/photos/view/1465574353/title
I am shown the correct page!
FYI: I'm using the same format for my other section, which looks like this:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?section=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?section=$1¤tpage=$2 [NC,L,QSA]
RewriteRule ^read/([0-9]+)/([\w-]+)/?$ read.php?id=$1&title=$2 [NC,L,QSA]
http://localhost/photos/view/1465574353 is already matched by
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1¤tpage=$2 [NC,L,QSA]
view - ([\w-]+)
1465574353 - ([0-9]+)
and therefore not handled by the final RewriteRule.
If you want it handled by the more specific (view) rule, you must swap the last two, because the rules are tried in order.
RewriteRule ^view/([0-9]+)/?$ view.php?id=$1 [NC,L,QSA]
RewriteRule ^([\w-]+)/([0-9]+)/?$ index.php?category=$1¤tpage=$2 [NC,L,QSA]
Related
hi guys can you help me here im having a little trouble here im tring to use the htaccess to remove all 20% in my url and replacing it with hyphen I manage to get rid the other 20% in between the words Acer,Liquid,S1,S510
here is my url /localhost/gadgets/product/Acer-Liquid-S1-S510%20Mobile
As you can see there is one %20 in last part, how can I remove it
And here is my htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /gadgets/
Options -Indexes
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1-$2 [NC,L]
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Thanks in advance guys
Try this:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /gadgets/
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=Redirect:1]
RewriteCond {ENV:Redirect} ^1$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1 [NC,L]
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The first rule removes all the whitespaces in a loop (using [N]) so we don't have to specify multiple RewriteRules doing it one by one now.
The next rule (with {ENV:Redirect} condition) is optional and is used to reflect the use of hyphens on the client's browser as well so that any bookmarks created link to the correct non-whitespaced version of the URL.
I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]
I am trying to rewrite the following url
http://localhost/foldername/index.php?url=writeboard/index&step=1&id=1
to
http://localhost/foldername/writeboard/index/step/1/id/1
Code is
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
I tried with
RewriteRule ^(.+?)(?:/(step)/([0-9]+))(?:/(id)/([0-9]+))?/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
it works but when the url becomes http://localhost/foldername/writeboard/index then I am getting 404.
This rule should work:
RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?url=$1 [NC,L,QSA]
You should also add RewriteBase /foldername/ to your .htaccess file, since you're not in the top level.
More information about RewriteBase can be found here.
I have this link:
index.php/forums/viewforum/5/
Now, I want that "forums" word in URL to become dynamic such that which ever word I replace it with, it still redirects to the same URL.
For example, if I have:
ProductA/viewforum/5/
it redirects to:
forums/viewforum/5/
For example, if I have:
ProductB/viewforum/13/
it redirects to:
forums/viewforum/13/
In other words, if there's a "view forum" word in the URL, it should trigger this rewrite.
I already have a .htaccess that removes the index.php from the URL so the rewrite rule should consider that too.
Is it possible?
HTACCESS:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule member http://%{HTTP_HOST}/404 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.+)$ /index.php/$1? [L]
</IfModule>
Your 2nd and 3rd rules look suspect.
Have your full code like this:
RewriteEngine on
RewriteRule member http://%{HTTP_HOST}/404 [R=301,L]
RewriteCond %{REQUEST_URI} !^/forums/ [NC]
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.+)$ /index.php/$1 [L]
I need help with this rewrite in .htaccess file.
So this what I have now ans this works but when I try to add a new RewriteRule nothing happens.
I the url that I want to be rewrite is index.php?page=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
So when I do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
The page doesn't have any css when i do it like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1
The page has css but i still get index.php?page=pagetitle. But the profile page does give me /username.
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1
Your are asking the server to redirect every URL to two different pages, it cannot work the server cannot just guess what page to load.
What you need is either a /profile/username rule or a /page/pagetitle rule.
IE something like:
RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]
Your rewrite rules are based on regular expressions and therefore need to be as specific as possible so the server can determine exactly which url to use - for example how can you tell if http://example.com/something is a page or a profile? Using a prefix such as "user", "profile", etc on your URLs means that http://example.com/profile/something can be redirected as as a username with a default redirect for everything else. To accomplish this you need to make the more specific pattern match first (users) and utilized the [L] directive to indicate that following rules should not be processed. I usually use a negative character class for URLs to match anything except a forward slash - [^/]*.
# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Test at http://htaccess.madewithlove.be/ (note that %{REQUEST_FILENAME} and %{REQUEST_FILENAME} aren't supported for testing).
Profile
input url
http://www.example.com/profile/something
output url
http://www.example.com/profile.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]
This rule was met, the new url is http://www.example.com/profile.php
The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
Page
input url
http://www.example.com/something
output url
http://www.example.com/index.php
debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
This rule was met, the new url is http://www.example.com/index.php
The tests are stopped because the L in your RewriteRule options