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
Related
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
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
I have a hosted Apache based website. Currently, we create a new project folder on the site, and dump the flash based course up for review. Is there a way to make the content folder expire after a certain amount of time so that the content is no longer view-able?
Please let me know if you need any further clarification.
Yes, you can, using mod_rewrite module.
For example, to deny all requests since November 2014 you could use these rules:
RewriteCond %{TIME_YEAR} =2014
RewriteCond %{TIME_MON} >10
RewriteRule .* /deny.html
RewriteCond %{TIME_YEAR} >2014
RewriteRule .* /deny.html
To see all the available environment variables check the link below.
PS: you obviously need to provide some nice /deny.html page so that it was obvious for visitor what happened.
Or if you just need a standard 404/403 use the following rule:
RewriteRule .* - [R=404]
References:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
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]
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 am trying to build a simple URL shortner and I have come across a few complications:
A) in .htaccess how would I rewrite mydomain.com to make it so users can enter mydomain.com/4854 while the page is actually mydomain.com/url.php?key=4854.
B) If I do have that Rewrite rule how can I be sure to bypass it for index.php and add.php
Thanks so much in advanced.
In your .htaccess you can do:
RewriteEngine On
RewriteRule ^([0-9]+)(/)?$ /url.php?key=$1 [QSA,L]
This rewrites requests that contain only numbers to url.php.
Then in url.php, if key is not found, redirect to error page.
RewriteEngine on
RewriteRule ^([0-9]+)(/)?$ url.php?key=$1
And if the key is invalid, redirect in the php file
Let me save you the grief...I was doing the same thing, and found it written for me at yourls.org. It covers pretty much everything you need, as well as giving you option user pages to add their own, AND gives you an API interface for others to use the shortened URLS. I had it up and running in minutes, and spent a couple of hours modifying the samples given to be ready for production.
Even if you don't use it, the mod_rewrite solution is there for you.
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