PHP (htaccess) rewrite with $_GET request - php

I made an simple framework for my website development.
I am currently using the this URL pattern:
www.example.com?r=account/login
But I would like to change it to:
www.example.com/account/login
remove r= from the URL.
I have seen YII framework does this. But I couldn't find a way to do this.
Solution:
Add the code below to your .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?r=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?r=$1
</IfModule>`

What you are looking for are SEO friendly URLs which are quite common to use & implement. This is the .htaccess from a basic WordPress install which achieves a similar goal:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Then in index.php you can test this by doing a variable dump of $_GET like so:
echo '<pre>';
print_r($_GET);
echo '</pre>';

I see you found a solution however you don't/shouldn't need two rewrite rules for the same thing just to add the /. You can use a ? to make it optional. I also recommend using [L] on the rewrites so that it stops when the rule is met.
This is more of what you were looking for.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?r=$1 [L]
</IfModule>

Related

Using .htaccess to rewrite urls with ID: "Page not found"

I am trying to rewrite some urls by using the .htaccess file in a WordPress environment. I have a WordPress page called "offerta" with two parameters, one of them is a custom ID that I use to generate content.
so, an example url is: http://www.riccionelastminute.it/rlm/offerta/?id=15&titolo=my-title
and I would like it to be: http://www.riccionelastminute.it/rlm/offerta/my-title-15/
this is my .htaccess right now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rlm/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /rlm/index.php [L]
</IfModule>
# END WordPress
After some research, I tried to add this rule right after the last Rewrite Rule, but it redirects to a 404 page:
RewriteRule ^offerta/([A-Za-z0-9-]+)-([0-9]+)/$ offerta/?id=$2&titolo=$1
I'm not used to play around .htaccess and I'm not an expert.
Am I missing something?
The problem is that the last RewriteRule has the L flag which means that no other rules are applied after it. What you need to do is add your rule before the last one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rlm/
RewriteRule ^index\.php$ - [L]
RewriteRule ^offerta/([A-Za-z0-9-]+)-([0-9]+)/$ offerta/?id=$2&titolo=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /rlm/index.php [L]
</IfModule>

mod_rewrite for seo friendly urls with 5 parameters

I have a page in the following location: /property/happy_property_urls.php
My goal is to make the happy looking url's look like this: /en/london/apartment/for-rent/ww123/
I have tried various methods but none of them seem to stick and I only get 404 errors.
EXAMPLE NOT WORKING
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /property/
RewriteRule ^(.*?)$ happy_property_urls.php?lang=$1&city=$2&type=$3&status=$4&ref=$5 [NC,L,QSA]
</IfModule>
I am sadly failing badly.
Can someone give me some pointers please?
You can use this rule in the .htaccess in the root.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ property/happy_property_urls.php?lang=$1&city=$2&type=$3&status=$4&ref=$5 [NC,L,QSA]
</IfModule>

Extra clean Wordpress clean urls

I'm using wordpress for a site and I added an extra php page that uses a query string.
The url I currently have is site.com/page/?query=stuff
and I would like it to be site.com/page/stuff
I understand how to handle a .htaccess ReWrite rule for normal php projects but I'm having a hard time working around wordpresses .htaccess rules which are:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Thanks in advance for the help.
So in any Wordpress install I've used it automatically identifies /search/query+terms the same as ?s=query+terms, so you really just need to redirect users to the more SEO version, in which case you just need to do the following:
Add the following just below RewriteBase /:
RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^$ /search/%1? [L,R=301]
So your final file will look like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^$ /search/%1? [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Hope this helps! If your Wordpress install doesn't identify /search/query+terms as a search then let me know and I'll see if I can help you further! You can test this by manually going to the url before you try this, for example: example.com/search/hello+world

Enter own .htaccess rewrite rules in wordpress

I want to write a own rewrite rule into my htacces. The redirection itself works fine but Wordpress validates the URI with $_SERVER["REQUIRE_URI"]. So the site Title is Changed automatically to page not found, the loop doesn't works and so on.
Is there anyway to do this?
If not is there a other possibility actually I only need a really simple redirect from "www.example.com/test/" to "www.example.com/t.php?a=1" So it's completely static i don't need patterns at all. I have already tried it with "frameset" but that looks pretty ugly and makes the process slow.
Thank you very much
Jakob
My .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# My Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Malta/$ http://www.reisekontrast.de/Index?malta=1 [QSA,L]
RewriteRule ^China/$ http://www.reisekontrast.de/Index?china=1 [QSA,L]
# WP Rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
We need your original .htaccess file. This is an easy rewrite (redirect), but wordpress already has URL Rewriting rules. To help you best would mean we need to work cohesively with what settings you already have running. Otherwise your are just going to break Wordpress but get your URL's working.
Also, as a primer question, does your server support full override in htaccess?
Based on your HTACCESS file, I would add the section for "Custom" Rules. That way it bypasses the Rewrite Conditions that are arbitrary in this type of rewrite.
Hope this helps! Let me know if I can provide more detail or other options.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Custom rules
RewriteRule /test/(.*) http://example.com/t.php?a=1 [NC]
# My Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Malta/$ http://www.reisekontrast.de/Index?malta=1 [QSA,L]
RewriteRule ^China/$ http://www.reisekontrast.de/Index?china=1 [QSA,L]
# WP Rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

HTAccess URL Rewrite affect GET variables

We’re using wordpress, and we have the following in the .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
We have a URL like http://subdomain.example.com/select-a-card/?q=fuel, and we want to rewrite it so that it becomes something like http://subdomain.example.com/fuel-card/
What do I need to add to the htaccess to do this? Also, will this affect the queries from running that uses GET variables?
nothing, just read the wordpress manual and edit your settings. checkout http://codex.wordpress.org/Using_Permalinks
--- EDIT
This can be done, but only if you have some sort of a standard markup of the url's you want to have rewritten. Ie. if you only want to rewrite the select-a-card url's, or only the url's with a syntax like http://site.com/uri/?q=something. But you if the syntax of the url's differ too much you will have to add a lot of rewrites.
In this specific case something like this should work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add your specifics below:
RewriteRule ^/select-a-card/\?q=(.*) $1-card/
RewriteRule . /index.php [L]
</IfModule>

Categories