I am using the following code to change all my pages from .php to a simple /
RewriteRule ^(.*)/$ $1.php [L]
Now come up as www.site.com/clubs/ or www.site.com/training/ instead of www.site.com/clubs.php and www.site.com/training.php
Now within clubs i want to show the clubs (using data from a database) and the page will show as www.site.com/clubs/my-club/
All the my-club data will be in the database including the link in a url field.
I can get this with the following code. However when i echo using the $_GET['club'] method it is showing up my-club.php instead of just my-club.
RewriteRule ^clubs/([^/]*)$ clubs.php?url=$1 [L]
The ending / in the url seems to get changed into .php
You can achieve what you need in different ways, however that's how I would do it.
Change rules order, like this:
RewriteRule ^clubs/([^/]*)/$ clubs.php?url=$1 [L]
RewriteRule ^(.*)/$ $1.php [L]
I added the trailing slash to the first rule, since I guess is part of the pattern. If I'm wrong about it you can just delete it.
If you want to append any query string from the original request URL you should use QSA together with L, so [L,QSA]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z\-]+?)/([0-9a-zA-Z\-]+?)?$ $1.php?url=$2
Whan are you run:
http://localhost/club/
result will be
http://localhost/club.php
Whan are you run:
http://localhost/club/my-club
result will be
http://localhost/club.php?url=my-club
Related
I have these rules that i want to make it work.
RewriteEngine on
RewriteRule ^/(view|show)/(ebook|lecture)/?$ page1.php?a=$1&b=$2 [L,QSA]
RewriteRule ^/(view|show)/(ebook|lecture)/([^/]+)/?$ page2.php?a=$1&b=$2&c=$3 [L,QSA]
RewriteRule ^/([^/]+)/-page-([0-9]+)?$ page3.php?a=$1&b=$2 [L,QSA]
My links are normally like this
www.domain.com/page1.php?a=view&b=ebook
www.domain.com/page2.php?a=view&b=ebook&c=title
www.domain.com/page3.php?a=title&b=6
and i want to turn them like the following look
www.domain.com/view/ebook //page1
www.domain.com/view/ebook/title //page2
www.domain.com/title/page-6 //page3
I've tried my rules but only the first one worked, But the page didn't load and the style was literally broken and not even a single image loaded or anything at all.
Try with:
RewriteEngine on
RewriteRule ^/?(view|show)/(ebook|lecture)/?$ page1.php?a=$1&b=$2 [L,QSA]
RewriteRule ^/?(view|show)/(ebook|lecture)/([^/]+)/?$ page2.php?a=$1&b=$2&c=$3 [L,QSA]
RewriteRule ^/?([^/]+)/page-([0-9]+)?$ page3.php?a=$1&b=$2 [L,QSA]
No leading slashes in left htaccess RewiteRule url (you can use RewriteRule ^(view...)
And you use only page- in your www.domain.com/title/page-6 //page3 link (not -page-)
Before anyone comments, I know there are a lot of posts created on this topic, but none of them seem to solve my problem, that is why I have started this thread.
So, I have a page in my website called project.php which is used in GET query like so: project.php?id=12 I want to have a .htaccess file that converts the given URL into localhost/MyWeb/project/id/12/. I've literally followed every single post regarding that topic but none of them seem to work.
Also, along with that, I want all my .php and .html files to be shown just with their names, i.e localhost/MyWeb/index.php/ becomes localhost/MyWeb/index/ and localhost/MyWeb/sub1/sub2.php becomes localhost/MyWeb/sub1/sub2/.
EDIT:
The reason why I did not add my work in first place was because I didn't think it would be any helpful. But here it is:
RewriteEngine On
RewriteRule ^([0-9]+)$ project.php?id=$1
RewriteRule ^([0-9]+)/$ project.php?page=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Firstly, you are operating out of a sub-directory (MyWeb), which means you need to set a RewriteBase. Also, you need to ensure that your .htaccess file is placed inside that sub-directory, and not in the localhost document root.
So, below RewriteEngine on, insert the folloeing line:
RewriteBase /MyWeb/
Next, you stated that you want to convert project.php?id={id} to project/id/{id}, but your code omits the /id/ segment. I also noticed that you have two rules, and that the second one contradicts your question, so I am only going to show you the change you need to make for the first rule, until such time as you clarify what the second rule is for.
To make the project URI work, change the very first rule to:
RewriteRule ^project/id/([0-9]+)/?$ project.php?id=$1 [QSA,L]
This will match the URI you want, with an optional trailing slash. I've also added the QSA flag which appends any extra query string parameters to the rewitten URI, as well as the L flag which stops processing if the rule is matched.
Next, to omit the .php or .html from your URIs, change the last three lines to the following:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [L]
When you make a request to localhost/MyWeb/index, Apache will check to see if localhost/MyWeb/index.php or localhost/MyWeb/index.html exist, and will then serve whichever one it finds first.
If you have both the PHP and HTML files, then the PHP one will be served, and not the HTML one. If you prefer to serve HTML files, then swap the two blocks around.
Unfortunately, I don't know of a good way to force a trailing slash for these, specifically because of the condition that checks for their existence. In other words, it won't work if you request sub2/, with the trailins slash because it would need to check if sub2/.php exists, which it does not.
Update: For added benefit, place these two blocks just below the new RewriteBase you set earlier to redirect the old URIs to the new ones whilst allowing the rewrites to the new URIs to still work:
RewriteCond %{THE_REQUEST} \/project\.php\?id=([0-9]+) [NC]
RewriteRule ^ project/id/%1/ [R=302,L,QSD]
RewriteCond %{THE_REQUEST} \/MyWeb/(.+)\.(php|html)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ %1 [R=302,L]
For reference, here's the complete file: http://hastebin.com/gacapesoqe.rb
I'm using this rewrite rule, which works perfectly fine
RewriteRule ^(.*)-S\.html\??(.*)$ /index.php?mod=home&action=stadt&stadt=$1&$2 [QSA]
It points URLs like /Albersdorf-Holst-S.html to their PHP URL args.
Now, I need to rename some of these names, that's why I want to put RedirectPermanents in place. Likt this on for example:
RedirectPermanent /Albersdorf-Holst-S.html http://www.gruppenunterkuenfte.de/Albersdorf-Holstein--S.html
I would expect, when putting the RedirectPermanent in front of the RewriteRule it will be executed first and the RewriteRule will then be applied to the result.
But what happens, is that the URL in the adress bar reads after the redirect:
http://www.gruppenunterkuenfte.de/Albersdorf-Holstein--S.html?mod=home&action=stadt&stadt=Albersdorf-Holst&
Why is the query string added? And why does this only happen, when I redirect first.
Using RewriteRule ... [L,R=301] instead of RedirectPermanent helped:
RewriteRule ^Albersdorf-Holst-S.html http://www.gruppenunterkuenfte.de/Albersdorf-Holstein--S.html [L,R=301]
The real content is at domain.com/view.php?id=image_id
I want to have it be accessible with domain.com/view/image_id
The following is currently there to get the .php file extensions out.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Appending the following to the htaccess doesn't work:
RewriteRule ^view/([A-Za-z0-9]+). /view.php?id=$1 [L, QSA]
Also, I am fine with having everything after view.php? (view.php?id=) get taken into the php file and from there I can separate everything out by slashes and implement their data.
I have looked at many solutions here on stackoverflow and elsewhere but either they do not apply to me or something is wrong with my server. Either way, I get an internal 500 server error. Thanks.
You need to place the view rule before the more general php extension rule:
RewriteEngine On
RewriteRule ^view/([A-Za-z0-9]+)$ /view.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Also, you don't want that space after the L,.
^view/([A-Za-z0-9]+).
^---single character wildcard
That . at the end there is matching/consuming one of your ID values, so if the url is
/view/foobarbaz
Your capture group will actually be foobarba, and the z gets chopped off, producing
/view.php?id=foobarba
Your 500 Internal Server Error is the result of a redirect loop.
This is because your rule will always be true, because you are redirecting back to the same page.
You need to exclude the resultant link by adding a rule with ^! before all the rest.
I was wondering if I'm supposed to use .htaccess to change a get variable to a value from the database (username).
so if there is
http://www.url.com/user.php?u=1
how do you conver it to
http://www.url.com/andrewliu
Thanks!
You need to change your user.php to take a username instead of a userid. Then you can use something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]
This passes the username through the u query string parameter, essentially: /user.php?u=andrewliu
Otherwise there's no way htaccess and mod_rewrite can know what the mapping is between user_id and username. Alternatively, you can write a database script and use RewriteMap to create a mapping for you:
RewriteMap usermap prg:/path/to/userscript
and in your htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=${usermap:$1} [L]
The last option, if you've only got, say, 5 users (or some small amount), you can make explicit rewrites:
RewriteEngine On
RewriteRule ^/?andrewliu$ /user.php?u=1 [L]
RewriteRule ^/?anotheruser$ /user.php?u=2 [L]
RewriteRule ^/?foousername$ /user.php?u=3 [L]
You need to use Mod-ReWrite. You can use this in your htaccess files but if you have access to your httpd.conf file it will prove to be quicker there.
You would ideally use the ?u=1 in your htaccess file and use that to find the the name and append the name onto the end. Otherwise you will have to search for the username and could be succesiptable to spelling mistakes etc etc.
RewriteRule ^/([0-9]+)/.*$ /user.php?u=$1 [L]
This is how Stackoverflow does it, try accessing this page without the number in the url and you will get a 404 page not found. Get the number right and what ever else you type after the forward slash will be exchanged for the correct words! This is much more convienent!
Use $GET in php to get the value of 'u'
Then use your logic to convert it to name.
Now just use redirect function to go to to that URL