Giving parameters to php with RewriteRule - .htaccess

I need a RewriteRule to call a php file with parameters specified in the URL entred
ex :
if the url entered
http://www.example.com/folder1/sub1
to make it call go.php?p1=folder1&p2=sub1

Try using
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)?/(.*) /go.php?p1=$1&p2=$2
Where RewriteCond %{REQUEST_FILENAME} !-f,!-d tell mod_rewrite to apply the rule just to paths not mapped to existing files and directories.
See also the official documentation.

Related

How to mod_write query param to path for arbitrarily long path?

I'm building a simple API with apache and I want to map /really/long/path/api/captions.php?caption_id=blah to /really/long/path/api/captions/blah. It's important to NOT have to specify a full path in the rewrite rule because I want this to work no matter where I deploy this code to. However, I can't find/figure out a working example of .htaccess rewrite rules that enable me to match based upon only the final part of the extension.
So, assuming that I have, say, captions.php in a dir called api, what .htaccess file do I need to include in api to accomplish this transform without having /really/long/path/ anywhere therein?
(I also want to be able to map /really/long/path/api/captions.php to /really/long/path/api/captions/ and /really/long/path/api/captions.)
I've tried all sorts of wildcard-like syntax; here's one of those non-working attempts:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?captions/(.*?)/?$ /captions.php?caption_id=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /captions\.php\?caption_id=([^\&\ ]+)
RewriteRule ^(.*?)/?captions\.php$ /captions/%1? [L,R=301]
Thanks!
Got there in the end. This is all that was needed:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^captions/?$ captions.php [NC,L]
RewriteRule ^captions/(.*)/?$ captions.php?caption_id=$1 [NC,L]

Replace items with RewriteRule using .htaccess

I'm trying to use the Url rewrite using "RewriteRule".
I would use this link to show a new article in my blog:
https://www.example.com/article/how-to-visit-italy-in-winter
The original script (including vars) is
https://www.example.com/post.php?idpost=how-to-visit-italy-in-winter
This is my .htaccess
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .$ https://www.example.com/post.php?idpost=$1 [L]
but it doesn't work, the browser redirect to:
https://www.example.com/post.php?idpost=
I'm trying to resolve about 2 days witout success :(
Thank you.
You rule looks fine except for two things . Your $1 back reference is undefined and empty as you are not capturing any URI. To capture parts of the uri you need to use Regex capture-groups (.*) .
2) You need to use an absolute path instead of the full url as your RewriteRule`s destination if you want to silently redirect the url.
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule article/(.+) /post.php?idpost=$1 [L]

Rewrite rule with two $_GET

My .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /art/art.php?galerie=$1
So thanks to this I can access the different galeries that exist :
website.com/art/sun or website.com/art/beach for example ( as long as the galerie exists otherwise returns a error message saying page doesn't exists)
This all works fine up to here .
But i needed to add a $_Get on the page also :
website.com/sun?row=2 for example.
But i cannot do this as sun?row=2 isn't a registered galerie ...
How do I get around this problem ?
You can use the QSA (Query String Append) flag to forword new query strings to the target :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /art/art.php?galerie=$1 [QSA]
Now This will rewrite
/foo?querystring
to
/art/art.php?galerie=foo&querystring

Temporary redirection from two directories deep to the root

I have this directory structure:
|-language-A\
|-language-A\program-A
|-language-A\program-B
|-language-A\program-C
|
|-program-A\
Currently program-A is not translated to the language-A but somehow Google was able to crawl this directory and now it's showing in the search results. I need to redirect language-A\program-A to program-A in the root ... not all the folders inside language-A just that directory. This is a temporary redirection as I will remove that rule once that translation is ready.
So basically it will be something like:
http://example.com/language-A/program-A/what-ever
To:
http://example.com/program-A/what-ever
The question really doesn't say much so I will assume all strings are fixed and there are no parameters to pass:
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/language-A/program-A/? [NC]
RewriteRule .* program-A [R,L]
Redirects temporarily
http://example.com/language-A/program-A
To:
http://example.com/program-A
All according to your question: I need to redirect language-A\program-A to program-A in the root...
UPDATED
According to question update:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/language-A/program-A(.*)? [NC]
RewriteRule .* program-A%1 [R,L]
Redirects temporarily
http://example.com/language-A/program-A/what-ever
To:
http://example.com/program-A/what-ever
/what-ever is optional and will be passed if exists.

htaccess url rewrite with search

i am trying to create an .htaccess file that will achieve the following
create a clean url like this www.mydomain.com/About-Us with page page.php?title=About Us
query a database with the parameters passed on the url like this www.mydomain.com/?search=abc and it pulls to page index.php?search=abc
this is my code so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^?search=([0-9]*)$ index.php?id=$1 ## e.g www.mydomain.com/?search=try
RewriteRule ^([A-Z]*)$ page.php?name=$1
## www.mydomain.com/About-Us
##ErrorDocument 404 PageNotavailabale
####Protect the system from machines with worms
RewriteRule (cmd|root)\.exe - [F,E=dontlog:1]
#### To hold and redirect css/images/js files
RewriteRule images/(.+)?$ images/$1 [NC,L]
RewriteRule css/(.+)?$ css/$1 [NC,L]
RewriteRule js/(.+)?$ js/$1 [NC,L]
Why not using this kind of url for your search engine : www.domain.com/search/abc ?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([a-zA-Z0-9]+)$ index.php?search=$1
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
Then, your access your pages with www.domain.com/<myPage>.
And your search engine with www.domain.com/search/<mySearch>
EDIT :
Please notice your rules doesn't allow a lot of params :
^?search=([0-9]*)$ allows only numbers (even an empty parameter)
^([A-Z]*)$ allows only uppercase letters (and also empty parameter)

Resources