so my url is currently:
http://www.mywebsite.com/search.php?keyword=stack+overflow
I'm wanting to have it accessible as:
http://www.mywebsite.com/?s=stack+overflow
Now I've also implemented rewrites for .php files as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I'm a little confused now because the search.php is being rewritten to just search but is still accessable with search.php, so when I create my rewrite rule do I rewrite search.php or just search?.. either way I've tried and failed to accomplish it lol.
What should I add to have my desired url? Help is much appreciated :)
It's impossible to change parameter name with mod_rewrite. The easiest way to achieve what you want is to add these lines to .htaccess:
RewriteCond %{QUERY_STRING} ^s=
RewriteRule .? search.php [L]
and modify search.php to react to s get parameter the same as it reacts to keyword.
An alternative is to keep .htaccess intact and add these at the top of index.php:
if (!empty($_GET['s'])) {
$_GET['keyword'] = $_GET['s'];
require __DIR__ . '/search.php';
exit;
}
Related
I was looking for a simple .htaccess configuration that just convert /some_uri to /some_uri.php. Most examples in community are more complicated than I require. I was trying the following, but it didn't work:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^(.*)$ ./$1.php
Please help. Thank you.
You need to make sure that the rule does not match itself. In other words, you need to make sure the rule does not match if the url already ends on php. Besides that you probably want to prevent that the rule matches if it already points to a file that exists, so that you can normally serve css/js/images without it trying to append php to it.
RewriteCond %{REQUEST_URI} !\.php$ #Does not end with .php
RewriteCond %{REQUEST_FILENAME} !-f #Requested url is not an existing file
RewriteRule ^ %{REQUEST_URI}.php [L]
Let's say I have this URL:
http://mydomain.com/app/editor/?id=59500
I would like to have this url to instead show the following in the browser address bar:
http://mydomain.com/app/editor/59500
...but have the PHP page ("http://mydomain.com/app/editor/index.php") remain the same. In otherwords, I still want to have this page to be able to execute $_GET['id']; and return "59500".
Would I use regex in HTACCESS for this? Any advice on the best approach and an example would be greatly appreciated.
You want to use a .htaccess rewrite for this. You can make the redirect happen only when the URL ends with a number (to avoid redirecting index.php).
The \d+ gets all digits, and the /? will allow URLS that either have a slash after the ID or not.
Something like:
<ifmodule mod_rewrite.c>
RewriteRule ^app/editor/(\d+)(/?)$ app/editor/index.php?id=$1
</ifmodule>
Try adding these rules to the htaccess file in the /app/editor/ directory:
RewriteEngine On
RewriteBase /app/editor/
RewriteCond %{THE_REQUEST} \ /+app/editor/(index\.php)?\?id=([0-9]+)
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index\.php?id=$1 [L]
I have a rewrite rule on my webpage.
RewriteEngine On
RewriteRule ^(.*) index.php?p=$1 [L]
I want it to work so it rewrites URLs like this:
http://example.com -> index.php
http://example.com/home -> index.php?p=home
http://example.com/lol -> index.php?p=lol
But when I use the following php code inside my index.php
print_r($_GET)
It gives this:
Array ( [p] => index.php )
It gives this same results in all URLs (I tried these: http://example.com, http://example.com/, http://example.com/about, http://example.com/about/
Can you help me debig this?
I got it figured out:
The correct code is this:
RewriteEngine On
RewriteRule ^([^.]+)/?$ index.php?p=$1 [NC,L]
Sorry for the question.
The issue is that your rewritten URL still matches the rule and you get a new rewrite:
http://example.com/home
http://example.com/index.php?p=home
http://example.com/index.php?p=index.php
Since the [QSA] flag is not set, the new p parameter replaces the previous one. (I'm not fully sure about why you don't get an infinite loop, I suppose mod_rewrite makes a check to avoid useless redirects).
You need to add additional conditions. For instance, you can make a rewrite only if the URL does not match a physical file or directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [L]
I'm somewhat new to htaccess rewrite rules, and have been scratching my head for the past few days on what's happening here. No amount of Googling seemed to help, so hopefully somebody knows the answer.
I have a site that can be accessed as:
www.site.com
www.site.com/684
www.site.com/684/some-slug-name-here
All of these scenarios should go to index.php and pass in the optional id=684 and slug=some-slug-name-here
Which works fine.
My problem is I have a separate file. Right now it's called admintagger.php - but this fails when I call it anything. 21g12fjhg2349yf234f.php has the same issue.
The problem is that that I would like to be able to access admintagger.php from www.site.com/admintagger
but it seems to be matching my rule for index, and taking me there instead.
Here is my code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^imagetagger$ /imagetagger.php [NC,QSA]
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
If you want to arbitrarily be able to access php files via the name (sans extension) then you need to create a general rule for it. But you need to be careful otherwise you may be rewriting legitimate requests for existing resources (like a directory, or a slug). Try this instead:
# make sure we aren't clobbering legit requests:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# see if appending a ".php" to the end of the request will map to an existing file
RewriteCond %{REQUEST_FILENAME}.php -f
# internally rewrite to include the .php
RewriteRule ^(.*)$ /$1.php [L]
Then you can have your routing to index.php right after that:
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
Although you may be better off create a separate rule for each of your 3 cases:
RewriteRule ^([0-9]+)/([^/]+)/?$ /index.php?id=$1&slug=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [NC,L,QSA]
RewriteRule ^$ /index.php [L]
Firstly, sorry for my bad English.
I want config my .htaccess to rewrite URL.
example.com/company1.com
instead example.com/sub=company1.com
My .htaccess now:
RewriteEngine On
RewriteRule ^([a-z_]+)/?$ index.php?sub=$1
I was search in stackoverflow.
If i using (.*) regex for all charaters or ([a-z\.]+) for include "dot" character in domain string ( company1.con), my skin was broken.
My temporary solution is use ([a-z_]+) with http://example.com/company1_com instead
http://example.com/company1.com
It's bad solution :(
So, please give me regex for this problem.
Thanks.
Rewriting for Apache is described in mod_rewrite.
For you, as long as you ignore possible GET-parameters or paths, it should be
RewriteEngine On
RewriteRule ^/([^?/]+) /index.php?sub=$1 [L]
I guess it was broken because either you were missing the "/" before index.php, there is a longer path in GET ( example.com/company1.com/css/style.css ) or you submit a form ( example.com/company1.com?a=foo&b=bar ).
You need to prevent the index.php from looping:
RewriteEngine On
# let index.php pass through, thus stopping the rewrite loop
RewriteRule index.php - [L]
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
You could also do a check for existing resources first. Since index.php exists, that would also break the loop. This would make it so if you're requesting static content like javascript or css, it won't get routed through index.php:
RewriteEngine On
# request isn't for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# route everything to index.php
RewriteRule ^(.*)$ /index.php?sub=$1 [L]
Try this one
RewriteEngine On
RewriteRule ^(.*)$ index.php?sub=$1