Mod Rewrite Help - hiding PHP get query [closed] - php

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm trying to remove a PHP Get query from my domain. For example, instead of showing example.com/?url=1234, I'd like it to rewrite to example.com/1234, hiding the query but not removing it. I know this is possible and have read many tutorials on how to do this, but my code just isn't working. Here's what I'm currently trying:
RewriteEngine On
RewriteCond %{QUERY_STRING} url=
RewriteRule (.*) http://example.com/$1? [R=301]
What this is doing is stripping the query entirely, instead of just removing the ?url= segment.

You are thinking about it the wrong way round. Rewriting is not something that the client sees, but something that are exclusive to the server.
This means that you can make example.com/1234 work as though the client had used example.com?url=1234.
To achieve this you would use the following lines:
RewriteEngine On
RewriteRule (.*) http://example.com/url=$1 [QSA]

Check this - htaccess rewrite for query string
and
Htaccess Querystring rewrite

You need to extract the relevant part of the query string in the RewriteCond line.
RewriteEngine On
RewriteCond %{QUERY_STRING} url=(.*)(&|$)
RewriteRule (.*) http://example.com/$1%1? [R=301]
The above will discard any other query string parameters that you give (see examples below).
It will also keep the filename if given.
Examples:
http://example.com/?url=1234 ----> http://example.com/1234
http://example.com/a/?url=1234 ----> http://example.com/a/1234
http://example.com/?url=1234&a=b ----> http://example.com/1234

Related

How to disable 404 errors and make every directory look like one even if it's not there? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
For a project, I need to make every directory (server.com/dir1, server.com/dir2) look like there is the directory, even when there is not. I could say I need a directory wildcard. Say the content would be somewhere on the server, and gets included in the directories. Is there a way to achieve this with .htaccess?
Thanks!
Try something like this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)/*$ ./yourActualScript.php?directory=$1
The regex part in brackets is your folder, the slash star is the rest of the url (if any), and the request will be sent to an actual script with GET var directory

Open image in a page if users try to access directly [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
One thing I've noticed on Facebook is when you access an image/video directly, you can view it, even if the uploader specifies that you're not able to see it.
So I was thinking. Is it possible to redirect any image that is accessed directly (eg. www.example.com/img.jpg) to a page that has that image on? So say you were to access www.example.com/img.jpg, it'd redirect to a specific page for that image (perhaps the page is named after the image or something, so like it'd redirect to www.example.com/img.php).
I imagine it could possibly be done with .htacces, but I'm not advanced with that.
Anyone ever achieved this before and know if it's possible?
Something like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteRule ^(.*)\.(jpe?g|gif|png)$ /$1.php [L,R]
redirects any request for an image that isn't referred to by your site (www.example.com) to the image's name but with a .php extension instead.
Of course, this only works if you have a php file named after each of your images. If you want just a single script to process all your images (might be easier if you have a lot of images), you can do something like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteRule ^(.*\.(jpe?g|gif|png))$ /image_page.php?image=$1 [L,R]
And you'll have a script image_page.php take the $_GET[] parameter "image". And you can output a page that links to the image.
If you'd rather not redirect the browser, just remove the ,R flag inside the square brackets.

apache url rewriting for random number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
the thing is that im capturing my url based on GET method... so my url looks like
localhost/test/index.php?vic=24
where vic is variable...
ALSO i have a button on my site that is randomizing my vic every time.
<a href="'.htmlentities($_SERVER['PHP_SELF']).'?vic='.randomVic($link).'">
note that randomVic($link) is a function that returns random number every time.
what i want to achieve is that my url looks like
localhost/test/index.php?vic=24 -> localhost/test/24
and so on for every random number.
i really need help on this, i tried numerous .htacces mods for rewrite (striping .php, removing index, ...) but none of them worked as i needed them.
Thanks!
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then put this code in your DOCUMENT_ROOT/vic.htaccess file:
RewriteEngine On
RewriteBase /vic/
RewriteCond %{THE_REQUEST} \s/+vic/index\.php\?vic=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([0-9]+)/?$ index.php?vic=$1 [L,QSA,NC]
RewriteRule ^test/([0-9]+)$ index.php?vic=$1
edited as per comment below

RewriteRule on PHP files and parameters [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm creating a website and I want it to be SEO Friendly.
I'm having extension of pages as .php and I want to hide it from url.
I'm also having two other pages names abc.php and xyz.php which gives output depending on ulr parameters.
So I want something like this:
mydomain.com/pagename.php to show as mydomain.com/pagename
mydomain.com/abc.php?id=name to show as mydomain.com/abc/name
mydomain.com/xyz.php?id=name to show as mydomain.com/xyz/name
Please help me to write RewriteRule in .htaccess for that.
You have two options here:
Option 1: Use MultiViews - then apache will pass requests to your php files even when the extension is omitted - and then deal with the rest inside php (parse $_SERVER["REQUEST_URI"]).
Option 2: Disable MultiViews and use URL rewriting:
Options -MultiViews
RewriteEngine On
RewriteRule ^(pagename|abc|xyz)/?$ $1.php [L,QSA]
RewriteRule ^(pagename|abc|xyz)/([^/]+) $1.php?id=$2 [L,QSA]

How to make form shows only value in the URL? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to build an online Malayalam to English and English to Malayalam dictionary.
There are two online dictionaries available, however it is not perfect so I have plan to build a good dictionary.
When I check some of the website both of them are used get method at the same time URL details are totally different method. Let me show how it working:
At the same time my website showing like this:
Is there any option to my URL like other two websites? I think both websites are using PHP with jQuery.
try this
RewriteEngine On
RewriteBase /
# make pretty urls work
RewriteRule ^dictionary/([^/]*)$ index.php?ml=$1 [L]
# redirect none-pretty urls
RewriteCond %{QUERY_STRING} ml=(.*)
RewriteRule ^$ /dictionary/%1 [L,R=302]
You'll also need some Javascript to catch the form's onsubmit, and change the url to not have the get parameters. You could do without, but that would result in an extra 302 request, and slow the pageload down a bit.
(PS make sure you php script return a 404 page if it can't find the word in the database.)
You need to use the mod_rewrite from apache2, it's a way for mask your parameters in a user friendly url.
You can read about thins in this tutorial:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
full docs: http://httpd.apache.org/docs/2.2/rewrite/

Categories