htaccess Multiple URL Variable clean URL - php

Ok i've tried multiple versions of code on my .htaccess file and I still can't get it to work properly... I don't know if I'm putting it in the wrong folder or what is going on but, here is what I'm trying to accomplish.
This is my code
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([a-z0-9]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
Here is another version that I've tried
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
Here is what I'm trying to accomplish... a URL that looks like this:
example.com/rentals/32746/florida
The "32746" and the "florida" come from a form on the index page of the site and then it gets passed over to the "rentals" folder through the URL.
The .htaccess code sorta works whereas it spits out a URL like this:
http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas
But do you see the extra on the tail end? It's as if it's duplicating the results in the URL.
I currently have the .htaccess file in my "rentals" folder should it be in the root folder?
UPDATED VERSION
For those who were running into the same problem as myself... here is the actual code I ended up going with:
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]

You need to add a ? to the end of the target path in your first Rule :
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
As it descards the orignal query strings from the destination.
Clear your browser's cache before testing this.

Related

RewriteRule get error when i call xajax

like a title i have a problem with this rewrite rule
I write this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(font|css|js|images|remote) [NC]
RewriteCond %{REQUEST_URI} !^/(font|css|js|images|remote/.*)$ [NC]
RewriteRule ^index$ /page/index [QSA,L]
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]
When i call ajax remote from /remote/check-function.php from pages he load full /page/post with variable sent by ajax.
How i can resolve this issue
Thanks in advance.
Best.
The RewriteCond only applies to the RewriteRule that comes immediately. Properly that the url /remote/check-function.php matches the last rule and is rewritten. You should add RewriteCond to check whether the request matches the existing files / directories before applying the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /page/article&category=$1&post=$2 [QSA,L]

.htaccess to search events with a nice looking url

I'd like code for a .htaccess file that when this is entered:
domain.com/event/granniesteaparty a file in the folder 'event' (display.php) takes the 'granniesteaparty' bit as though domain.com/event/display.php?search=granniesteaparty had been entered.
If tried all sorts of things but this is where I am at the moment:
RewriteEngine on
RewriteRule ^event/([A-Za-z0-9]+)/([0-9]+)/?$ /event/display.php?search=$1 [NC,L]
RewriteCond %{QUERY_STRING} s=([^&]+)
RewriteRule ^event /event/1? [NC,R=301]
I hope that makes sense, could someone point me in the right direction please?
You have too many capture groups in your first rule. Give this a try
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ /+event/display\.php\?search=([^&\s]+)
RewriteRule ^ /event/%1? [R=301,L]
RewriteRule ^event/(\w+)/?$ /event/display.php?search=$1 [NC,L]
Put the following code .htaccess inside event directory:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+event/display.php?search= [NC]
RewriteRule ^(.*)$ event/display.php?search=$1 [R=302,L,NE]
RewriteRule ^event/display.php?search=$1(.*)$ /$1 [L,NC]
Update
If you want only to redirect any wrong page to the display page but internally so the following code will do it :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) event/display.php [L,NC]
So when someone request /event/whateverwrong it will be at the url as it is but internally will map to display.php

htaccess - Redirect EVERYTHING through php file

RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ api.php?url=$1 [L]
Above shows my current configuration.
The problem with this is it only seems to be redirecting if a directory or file exists after the folder the htaccess file is in.
How can I rewrite this so it literally just redirects every single request to api.php with the full requested path? I'm not looking to see whether directories or files exist in the htaccess file, I can do that in api.php, I just want to push all the requests through that file.
You can use:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^((?!api\.php$).*)$ api.php?url=$1 [L,QSA,NC]
Lookahead (?!api\.php$) means rewrite everything except /api.php to /api.php with query parameter url.
like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /api.php?path=$1 [NC,L,QSA]
make sure you have this before the route
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Creating a Pretty URL

It almost is similar to how to create pretty urls but, it does not seem to work for me.
Here is my url:
http://localhost/pr/ajax/ajax_load.php?task=get_blob&blid=199
How do I convert it to:
http://localhost/pr/ajax/get_blob/199
Here is my .htaccess:
RewriteEngine On
RewriteBase /pr/ajax/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+pr/ajax/ajax_load\.php\?task=([&\s]+)&blid=([0-9]+) [NC]
RewriteRule ^/%1/%2? [R=301,L,NE]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ /pr/ajax/ajax_load.php?task=$1&blid=$2 [L,NC,QSA,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|gif|png|pdf)$ /pr/ajax/%{REQUEST_URI} [NC,L,R=302]
Where should the .htaccess be placed? I mean, does it matter where you put it at? My root is at pr.
The location of the .htaccess file is important, specially for the URI-path tested in the rewrite rule, that contains only the path segment after the directory where the file is located. To overcome that limitation, only the REQUEST_URI variable is used in the following code.
Here is a tested solution that works with .htaccess file at either /pr or /pr/ajax/ directories:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /pr/ajax/ajax_load\.php\?task=([^&]+)&blid=([^&\s]+) [NC]
RewriteRule .* /pr/ajax/%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !ajax_load\.php [NC]
RewriteCond %{REQUEST_URI} ^/pr/ajax/([^/]+)/([^/]+)/? [NC]
RewriteRule .* /pr/ajax/ajax_load.php?task=%1&blid=%2 [L,NC]
Try this:
RewriteEngine On
RewriteRule ^/pr/ajax/(.*)/(.*)$ /pr/ajax/ajax_load.php?task=$1&blid=$2 [NC,L]
And yes, it does matter where you put the htaccess file. You put the file in the directory of the file whose path you want to modify/manipulate.

CodeIgniter Routing

I'm trying to build a short URI service with CI just so I can learn CI faster
anyway .. I got stuck at the routing
i hid the index.php then added the following route $route['([A-z0-9]{4})'] = "/forward/redirect/$1";
but it just shows my default controller
I also tried with HTaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
It gives an error for not having any passed data
any help is appreciated.
Cheers
Since there is no physical path /forward/redirect/, you should redirect to the "catch all" index.php file in the root and input the path as parameter:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /index.php/forward/redirect/$1 [NC]
Or you can leave the rule as is and append another rule (this way you will have two rewriting cycles, the first one will rewrite to /forward/redirect/asdf and then the second one rewrites to index.php/forward/redirect/asdf:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ /forward/redirect/$1 [NC]
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
i finally got it working >_>
routes file contains this
$route['([A-z0-9]{4})'] = "/forward/redirect/$1";
htaccess contains this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-z0-9]{4})$ index.php/forward/redirect/?$1 [NC]
RewriteCond $1 !^(index\.php|images|robots\.txt|(.*).js|(.*).css|(.*).jpg|(.*).png)
//added (.*) so resources could be loaded properly :)
RewriteRule ^(.*)$ /index.php/$1 [L]

Categories