RewriteRule on PHP files and parameters [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 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]

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

How can I make a folder expire and not serve up its contents after period of time? Using Apache [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
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

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

URL rewirte directory names [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 9 years ago.
Improve this question
I have my website directory like this:
application
assets
system
And under application I got this:
help
contact
I created a file inside each one called index.php so the URL can look something like this, but I have seen so many websites with an URL like this, I've seen this is made with .htaccess file, but the problem is that I don't know where should I put it and how to set it up.
Would be nice if you put some examples.
The apache site has tons of examples.
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
This isn't tested, but something like this should suffice.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/application/
RewriteRule ^(.*)$ /application/$1 [L,QSA]

Mod Rewrite Help - hiding PHP get query [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 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

Categories