How do I re-write URLs with htaccess in a WAMP? - php

Could someone please tell what I am doing wrong. I have searched through lots of posts here and elsewhere about doing this.
Just want to make localhost/index.php?page=somepage look like localhost/somepage
Here is what I have so far:
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([0-9a-zA-Z\-]+)$ index.php?page=$1
</IfModule>
Not getting any errors, but it's not dong anything actually. Maybe I have the code right but just can't do this on my system. But would like a pro opinion.
Thank you.
Maybe worth mentioning: have htaccess file in root dir, all pages are in a folder named "pages" and all end in .htm

That looks like it takes a request like: localhost/somepage and internally rewrites it to localhost/index.php?page=somepage, which is really what you want. But it doesn't address if someone tries to go to localhost/index.php?page=somepage. In order to address that, you don't want to rewrite URL's, you want to redirect the browser to a new URL:
RewriteCond %{THE_REQUEST} \ /+index\.php\?page=([^\ \&]+)
RewriteRule ^ /%1? [L,R]

Related

.htaccess url rewriting not working?

I'll be honest in saying I have very little experience with .htaccess as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1. The rest is used to remove the page extensions from the urls. I've changed $1 to $2 thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Change order of your rules and use MultiViews option. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

.htaccess being ignored? Not redirecting like it's supposed to

Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.

Convert PHP Variable URL with mod_rewrite to clean URL

I need to convert a link that contains php variables into a clean link that can be picked up and indexed by Google. I've looked at ways to do this, and using mod_rewrite apparently is the best way to handle this.
Currently my URLs look like this:
http://mysite.com/jobs/view/?j=senior-model-validator-groep-risk-management
I'd like to end up looking like this
http://mysite.com/jobs/view/senior-model-validator-groep-risk-management
It's probably really easy but sadly I know nothing about .htaccess. I tried a few things but I end up with nothing remotely close :-)
Thanks in advance!
Edit: Here's my full .htaccess file based on the help so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You may use an .htaccess file at the root of you web site with these rules:
RewriteEngine on
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
Do not forget to filter/validate GET input in the script dealing with the data...

Redirecting admin/??? to admin.php/??? with .htaccess

Here's my htaccess file at the moment:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /blog/
ErrorDocument 404 /blog/404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ post.php/$1.html
RewriteRule ^index$ index.php
RewriteRule ^archives$ archives.php
RewriteRule ^about$ about.php
RewriteRule ^search$ search.php
RewriteRule ^feed$ feed.php
At the moment if I go to domain.com/blog/*.html, it is redirecting (in the background) to domain.com/blog/post.php/*
I would like to almost copy this but I'm not sure about all this regular expression stuff.
I would like to be able to also go to domain.com/blog/admin/* and for it to redirect (in the background) to domain.com/blog/admin.php?p=*
If you guys could help me out, I would really appreciate it!
Thanks
Something like:
RewriteRule ^/?admin/(.*)$ admin.php?p=$1&%{QUERY_STRING} [L,NC]
Will do the trick.
This is done very quickly and it isn't tested but it gives you the idea. You put this above:
RewriteRule ^(.*)\.html$ post.php/$1.html
Edit: Took out the rewritecond since I just realised it is useless
Edit: Added query string too since you are actually converting the _GET now.
Add multiviews to the first line of your .htaccess:
Options FollowSymLinks MultiViews
Then, restart apache. It should point admin to admin.php.
Note: Don't have multiple files in the same directory with the same name and different extension.
http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html

Simple mod_rewrite in existing website

I'm doing a website (for portuguese speaking countries) that displays phrases one at a time.
I would like to have simpler non-dynamic addresses for the site to be SEO friendlier and easier to memorize to the user. So my problem is this, and thanks in advance for all the help possible:
nfrases.com/ -> completely random phrase (don't need changing this!)
nfrases.com/index.php?id_frase=2222 -> phrase with ID (wanted this to be nfrases.com/2222)
nfrases.com/tag.php?tag_nome=amor -> random phrase with tag_nome (want: nfrases.com/amor)
nfrases.com/tag.php?tag_nome=amor&id_frase=2222 -> specific phrase with tag=amor (want: nfrases.com/amor/2222)
I think this is a noob question about mod_rewrite but that's exactly what I am! :D Already made several tries with the few knowledge I have but either i got 404 pages either the parameters wouldn't be received by the $_GET['tag_nome'] or $_GET['id_frase'].
The actual htaccess I have is this but it isn't working:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.nfrases.com$
RewriteRule ^(.*)$ http://www.nfrases.com/$1 [R=301]
RewriteRule ^([0-9]+)(.*) index.php?id_frase=$1
RewriteRule ^(.*)/([0-9]+) tag.php?tag_nome=$1&id_frase=$2 [L,QSA]
Can you guys help me?
Thanks again for everything! I appreciate your help.
The following rewrite rules work for me on a RewriteRule tester:
#removes trailing slash
#/vida/222/ becomes /vida/222
#/222/ becomes /222
RewriteRule ^(.+)/$ /$1 [R=301]
#/222 silently rewrites to /index.php?id_frase=222
RewriteRule ^([0-9]+)$ index.php?id_frase=$1 [L]
#/vida silently rewrites to /index.php?tag_nome=vida
RewriteRule ^([a-zA-Z-]+)$ index.php?tag_nome=$1 [L]
#/vida/222 silently rewrites to /index.php?tag_nome-vida&id_frase=222
RewriteRule ^([^/]+)/([0-9]+)$ tag.php?tag_nome=$1&id_frase=$2 [L]
You may want something like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
In this case, nfrases.com/2222 will rewrited as index.php/2222. This easily allows base routing in your index.php file. For more advanced routing, it may be faster and easier to use a good framework. Take a look at FuelPHP (http:www.fuelphp.com) or CodeIgniter (http://www.codeigniter.com).

Categories