I am creating a CMS and having trouble with my .htaccess file, the line following
RewriteRule ^([-a-z]+)*/([-a-z_]+)*/$ ./page.php?page=$1&order=$2
will not work no matter what...
What am I doing wrong?
P.S. Here is my full code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([-a-z]+)*/$ ./page.php?page=$1
RewriteRule ^([-a-z]+)*/([-a-z_]+)*/$ ./page.php?page=$1&order=$2
RewriteRule ^blog-entry/([-a-z-0-9]+)*/$ ./single.php?post=$1&page=blog
RewriteRule ^blog/(\d+)*/$ ./page.php?page=blog&num=$1
You should put those last 2 rules first, as they are more specific, and the prior 2 rules will match before the latter ones.
Related
I've established a semi-working .htaccess file which alters this link:
localhost/profile.php?id=6
to this:
localhost/profile/6
However, this is adding .php to the end of my $_GET variables. I'm not very familiar with rewrite modules, .htaccess etc but this is very annoying. Couldn't find anything on the internet about it, if there is a different way to do it or fix it I would greatly appreciate any input.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*) profile.php?id=$1
</IfModule>
Try restricting to only numbers:
RewriteRule ^profile/(\d*) profile.php?id=$1
Or exclude ., if the ID is not certainly numeric:
RewriteRule ^profile/([^.]*) profile.php?id=$1
I have my url as
http://public_html.com/sub-products.php?catid=42
I want it to look like
http://public_html.com/sub-products/catid/42
I have made a .htaccess file too. but still there is no change in my url
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^sub-products/catid/([0-9]+)/?$ sub-products.php?catid=$1 [L]
Do i need to change anything in my php file also?
what code is required for php file to use .htaccess file?
Your rule in .htaccess rewrite only incoming seo-pretty URls to URL with php script filename. But if you need construct url as /sub-products/catid/42 you have to change you PHP code, that render HTML output.
for example
while ( $products as $product ) {
echo '<a href="/sub-products/catid/"'.$product['id'].'>'.$product['name'].'</a>;
}
Maybe there is a purpose to your line
RewriteRule ^(.*)$ $1.php
But i can't see it for the need you have, so i guess a
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sub-products/catid/([0-9]+)/?$ /sub-products.php?catid=$1 [L]
should work.
Edit :
This should work if you are trying to access the http://public_html.com/sub-products/catid/42 url
(or http://public_html.com/sub-products/catid/42/ btw)
If what you want is going from
http://public_html.com/sub-products.php?catid=42
to
http://public_html.com/sub-products/catid/42/
You might need something like this :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^catid=([0-9]+)$
RewriteRule ^sub-products.php$ /sub-products/catid/%1/? [R=301,L]
And then, you can use what i said before.
I am not sure if you switched your first statment around, but it looks in your .htaccess file you want to rewrite the other way around. Maybe this would work.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} catid=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/catid/%1 [R,L]
If you want to have a more generic rule, and not just for catid this RewriteRule could work:
RewriteCond %{QUERY_STRING} ([a-zA-Z]+)=([0-9]+)$
RewriteRule ^/?sub-products.php /sub-products/%1/%2 [R,L]
Hope it helps you forward.
EDIT: Made some changes after testing it on my system.
I am trying to rewrite the url using htaccess and I have tried answers given on another questions here however nothing seems to be working at all I still get the original url.
this is what I have:
http://localhost/inbox.php?pg=2
I want
http://localhost/inbox/2
I already had a rule that gets rid of the .php extension in .htaccess as below and just added the last line
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
RewriteRule /(.*)$ /inbox.php?pg=$1//added this
Your problem is that the line before the last one is defined as the last one so your rule must be above that RewriteConditions. Better use this rule set:
RewriteRule ^/?inbox/(\d+)$ /inbox.php?pg=$1 [QSD,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
I added your needed prefix which you missed and made it mandetory that after that numbers will follow (at least one).
Apache rewrite engine is mainly used to turn dynamic url’s such as http://localhost/inbox.php?pg=2 into static and user friendly url’s http://localhost/inbox/2
RewriteEngine on
RewriteRule ^inbox/([^/.]+)/?$ /inbox.php?pg=$1 [L]
Explanation: How this work
Call to action: RewriteRule
Pattern: ^inbox/([^/.]+)/?$
Rewrite: /inbox.php?pg=$1
Command Flag: [L]
Simply,
RewriteEngine on
RewriteRule ^inbox/([0-9]+)/?$ inbox.php?pg=$1
Source: 10 Simple examples to rewrite using htaccess
Currently I have a url
www.mysite.com/folder/details.php?d=sample.com
and I want it to change like this one
www.mysite.com/folder/sample.com
This is my .htaccess file, but it is not working:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ details.php?d=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ details.php?d=$1
I also tried this one, but its not working also:
RewriteEngine On
RewriteRule folder/([0-9]+) details.php?d=$1
This is my url link php code:
<?php echo $var['var']; ?>
I tried some of the answers I found in this site but still I cannot figured out what I am doing wrong. Anyone?
Thanks!
You need some conditions to prevent looping:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_.-]+)/?$ details.php?d=$1 [L]
You can also combine the two rules into one by using the /? at the end to make the slash optional.
For the rules to be placed in the document root, you need to add the folder names to the pattern and the target:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/([a-zA-Z0-9_.-]+)/?$ /folder/details.php?d=$1 [L]
Thanks for your comments #Jon Lin and #quasivivo , I found a solution to my problem, this is my htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^portfolio/(.*)$ /portfolio/details.php?d=$1 [L,QSA,NC]
and I also change my url link code:
<?php echo $var['var']; ?>
Hope this will help someone in the future.
I'm trying to make clean URLs for my application. So I wrote the codes below in a .htaccess file.
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/?$ index.php?p=$1&id=$2 [NC,L]
RewriteRule ^(.*)/?$ index.php?p=$1 [NC,L]
As you can see, I may have at most 2 variable in query string; p and id. If I have just one rule (the rule with 1 variable) in .htaccess file, everything will be fine. But when I add the first rule (the rule with 2 variable), it seems that all css, js, and image files will be go away.
I wrote css, images, and js files in this way :
./directory_name/file_name
Please help me to make clean URLs with multiple rules.
If you're following a Front-Controller Pattern, (i.e. everything goes through index.php) I'd encourage you to use FallbackResource.
FallbackResource /index.php
It's more concise and performant than the mod_rewrite equivalent (as seen in anubhava's answer):
Change your code to:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?p=$1&id=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ /index.php?p=$1 [QSA,L]