I'm needing some help with my .htaccess file. What I have are pages with the query page=''. I've tried looking up some resources on rewriterule's but haven't found what I need.
So what I'm going for is like:
domain.com/sub/index.php?page=options to domain.com/sub/options.
futhermore I'd really like a trailing / like:
domain.com/sub/options/
Any help or resources to point me to would be appreciated.
Edit:
Have tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?page=$1 [NC,L]
which seems to rewrite everything back to my root index.php
For the redirection, try this:
RewriteEngine On
RewriteBase /sub/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L]
For the trailing slash, you need to add it in your anchors on your pages.
Related
I have this url:
www.mywebsite.com/news/best-ever-phones
The part best-ever-phones is variable and could be anything.
index.php file is checking database for article with url best-ever-phones
The problem is that in ./news/.htaccess file I have this code:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?article=$1 [L,QSA]
which is supposed to load index.php file but server returns error 404 Not found when I visit www.mywebsite.com/news/best-ever-phones url.
What is wrong?
Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?article=$1 [L]
I think the problem may be with Rewrite base and your relative path. But this is how i'ld do it.
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteRule ^news/([A-Za-z-.]+)/?$ news.php?article=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
This should fix you up, rename your index to news.php
Perhaps give this a shot:
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-\/]+)\/?$ index.php?article=$1 [L,QSA]
This is presuming that you want the / character to appear in your $article variable, so you can logically separate different parts of the friendly URI if you want to in /news/index.php. Otherwise, single segment friendly URIs like /news-item-1 or whatnot can end with a trailing slash or not.
Really you don't want trailing slashes, as it might result in duplicate content penalties for loading the same page with a different URL (the / counts), so you could either not match \/? outside of the capture group or 301 redirect in index.php to the URL with the trailing slash removed. This way you also have clean query arguments, e.g. /news/something-big?orly=yes instead of /news/something-big/?orly=yes
I am using this site to test the rewrites. Otherwise untested as we don't have your code handy, so no way to tell whether it would work or not in your environment.
This code is used when we click on any url it will hide its extension.
But If you want to goto index.php page then write
echo 'Index Page ';
?>
I will redirect this url with help from mod_rewrite
http://www.example.org/site/asd
to
http://www.example.org/index.php?site=asd
On my webserver, mod_rewrite is enabled, but my example didn't works: (.htaccess)
RewriteEngine on
RewriteRule ^(.*)/site/$ index.php?site=$
it makes nothing, no fail but it didn't works.
Lets break it down
RewriteRule ^(.*)/site/$ index.php?site=$
The rule is only going to match urls like:
http://example.com/yada/yada/site/
http://example.com/something/site/
Which is backwards to what you want, so use a rule like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^site/(.*)$ index.php?site=$1 [L,QSA]
Which will match rules like:
http://example.com/site/yada/yada/
http://example.com/site/something/site/
http://example.com/site/no/slash
http://example.com/site/with/params/?abc=efg
Try rewriting as,
RewriteRule ^(.*)/site/([a-z]+)$ index.php?site=$1 [L]
Hoping someone can help me here. In my .htaccess file, I've got a rewrite rule written as:
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
for a URL that would be something like
localhost/wrestling.php?id=something
And now what I'm trying to do is change a URL like
localhost/category.php?id=something
to
localhost/id/category
The thing to note with this scenario is that this time I'm trying to use the id part as the thing after the first "/" and then add the category part after the second "/".
Below is what my entire .htacces file looks like at the moment.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Any thoughts guys?
You can try
RewriteRule ^([^/]*)/category/?$ /category.php?id=$1 [L]
Try this :
RewriteRule ^([^\/]*)/([^\/]*)$ /$2.php?id=$1 [NC,QSA]
You need two captured parameters.
This expression was tested with http://htaccess.madewithlove.be/.
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.
Ok, I have a link http://www.cuadmemo.com/posts.php?id=2861 and want it to show like this
http://www.cuadmemo.com/2861
I have in the .htaccess file this.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ posts.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ posts.php?id=$1
Have tried a number of different solutions that I read on this site but none seem to work this far. Any suggestions to fix this would be greatly appreciated.
RewriteEngine On
RewriteBase /
# Make sure we don't rewrite directories and existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ posts.php?id=$1 [L]