Clean URL with htaccess. no query string variable received - php

I am working on urls of a site. I want to convert the following url into
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
this url
http://dev.steelogic.com/solution/metalbuildingproduct
this is the htaccess code I am using.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/(.+)$ $1.php?id=$2
I don't know anything about htaccess. usually I just copy paste the text and it works.
the url with ?id= works fine
but when try to use the 2nd url it doesn't have any id.
I have to do this to many other pages as well and the all contain [.php?id=] which I want to replace with [/] slash

Try this rule in your DocumentRoot/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?id=$2 [L,QSA]

Please try this:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteBase /
RewriteRule ^solution/([^/.]+)/?$ solution.php?id=$1 [PT,L,NC,QSA]
</IfModule>

Because you say that even without this rule, going to http://example.com/solution/asdf loads /solution.php on your server, something else must be working on your url. Because you say you didn't define such a rule, it must be coming from somewhere else. Either a rule is defined somewhere else (main config file) or MultiViews is enabled. If you can't apply anubhava's solution (disabling MultiViews), move your file somewhere else, for example under /public/solution.php, so that url never partly matches an existing file, then use the following rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/$1.php -f #partly copied from anubhava's answer
RewriteRule ^([^/]+)/([^/]+)/?$ /public/$1.php?id=$2 [L]

from all of the discussion I concluded that its difficult enough to do it with .htaccess as there might b some sort of module working on the urls of site from the hosting and I get both solution and solution.php directed to same page
so I did it in php for now.
as both of the following pages
http://dev.steelogic.com/solution.php?id=metalbuildingproduct
and
http://dev.steelogic.com/solution/metalbuildingproduct
being going to same pages but with the second url I was not getting query string var. so I wrote a PHP code as I knew on each page how many vars I need so it worked for me. although it may not be a good approach but it works
$URI = explode("/", trim($_SERVER['REQUEST_URI']));
$numElems = count($URI);
if ($numElems <= 4) {
$arr = array();
$arr['id'] = $URI[2];
if (!empty($URI[3]))
$arr['id2'] = $URI[3];
}
if any body can help me with .htaccess I will appreciate.

Related

Can not Rewrite a URL with my .htaccess file

I'm having trouble rewriting a URL on my website to make it more presentable and easier to find on search engines.
I want to turn this:
http://www.gamingpopulace.com/threads/index?threadName=Glitches
Into this:
http://www.gamingpopulace.com/threads/Glitches
I'm currently using a .htaccess file with this in it:
RewriteEngine On
RewriteRule ^threads/([A-Za-z0-9-]+)/?$ /threads/threadName=$1 [NC,L]
From what I've seen seen in other questions, this should work.
What goes wrong:
When I type http://www.gamingpopulace.com/threads/Glitches into the URL, it just gives me a 404 error saying that the page is missing. From my understanding it should load http://www.gamingpopulace.com/threads/index?threadName=Glitches, but with the changed URL. Though I might be misunderstanding that.
Any help is appreaciated.
Thanks
Hello and welcome to SO
(I cheated and used Google but amended the info according to your question.)
Source : How To Set Up Mod_Rewrite
RewriteEngine on
RewriteRule ^thread/([A-Za-z0-9-]+)/?$ threads/index?threadName=$1 [NC]
Remove / from this ([A-Za-z0-9-]+)/?$
Replace
RewriteEngine On
RewriteRule ^threads/([A-Za-z0-9-]+)/?$ /threads/threadName=$1 [NC,L]
with
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^threads/([^/]*)?$ /threads/threadName=$1 [L]
I am assuming your original url is working, try this in root dir,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^threads/([\w-]+)?$ threads/index?threadName=$1 [QSA,NC,L]

add page name in the url using .htaccess

I just started .htaccess so I do not know too much about it, I try to google it but failed so I post my question here, I am sorry if the question is silly for you. My question is.
I use this code in .htaccess page:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
when I enter this url:
http://localhost/learn/php/htaccess/page/number
it works fine. but I also want to add index.php (page name) in the url. I tried this:
http://localhost/learn/php/htaccess/index.php/page/number
AND
http://localhost/learn/php/htaccess/index.php?page/number
but failed. How can I do this?
I simply want to do that if I add another page then I am not sure but I have to use this code:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
RewriteRule ^(.*)/(.*)$ another.php?x=$1&y=$2
but how can I open the another.php page with this format?
thanks
Try adding a few conditions to your rules:
RewriteEngine On
RewriteBase /learn/php/htaccess/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?n=$1&p=$2
As for routing to another page, you'll need to distinguish the difference between the two routes. For example, given a url:
http://localhost/learn/php/htaccess/foo/bar
where should it get routed to?
index.php?n=foo&p=bar
or
another.php?x=foo&y=bar
?
htaccess knows nothing about the content, only the URL and a regex pattern to match it, so unless you differentiate between the two, you can't route the same thing to two different scripts.
You can just make it easy like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?data=$1 [QSA]
in your index.php you put this code
$data = explode("/",$_GET['data']);
$page = $data[0];
$number = $data[1];

.htaccess mod_rewrite for pretty urls not working

Trying to get www.example.com/test.php?archives=testing to www.example.com/archives/testing
According to godaddy they have mod_rewrite enabled by default
Here is my .htaccess file:
rewriteengine on
rewritecond %{HTTP_HOST} ^example.com$
rewriterule ^example\/(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
However this is not working, when i go to www.example.com/archives/test I get a 404, suggestions
I just left this in a comment but i might as well put it here so its seen easier. I wrote an answer for someone thats helped others out over time, and in the end this isn't exactly an answer to what your asking however its more of a stepping stone in the direction. The original question was asked how to work with short url strings and make them work in a fashion like your looking for, but rather copy and paste that answer here. Ill let you go there and read over it.
Its not to go without saying you will need to alter the rule a little for your specific needs but it will in the end serve its purpose for getting you where you want to be.
PHP dynamic DB page rewrite URL
You need some rewrite conditions to specify when this rule will be used. Without them, you will keep running the same rewrite rule indefinitely, giving you an error. Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(\w+)$ test.php?archives=$1 [L]
How about this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^archives/(.+)$ /test.php?archives=$1 [L]
</IfModule>
Without is being super clear what you are trying to get from your rewrites I suggest:
Options -MultiViews
ErrorDocument 404 default
RewriteBase /

MOD_REWRITE & PHP $_SERVER['REQUEST_URI'] ISSUE

** UPDATED EXAMPLE **
For example, lets say my website url is "http://www.mydomain.com".
The site uses $_GET variables to determine pages, content etc.
So, let's say i'm on http://www.mydomain.com/cms/index.php?p=about-us
This would be the about us page within the CMS.
If I echo out $_SERVER['SERVER_NAME'] here, I get "http://www.mydomain.com"
If I echo out $_SEVER['REQUEST_URI'], I get "cms/index.php?p=about-us".
So, with the code below, the "$lastPage" gives me:
"http://www.mydomain.com/cms/index.php?p=about-us", this is exactly what I want.
BUT, as soon as I submit a form, or update content, etc, i'm being redirected to a css file, or an image path.
This ONLY happens when I have the rewrite rule in the htaccess file, which I need in order to have nicely formatted urls.
ORIGINAL QUESTION
I am running a website on a CMS, and within the CMS there is a "last page" variable. This variable is created like this:
$lastPage="";
$lastPage .= $_SERVER["SERVER_NAME"];
$lastPage .= $_SERVER["REQUEST_URI"];
$_SESSION['theLastPage'] = $lastPage;
The variable is used to redirect users to the last page they were on after updating content, submitting forms etc.
It is used like this:
<script>window.location="<?php echo $_SESSION['theLastPage']; ?>"</script>
This works exactly how I want it to...
Until I add this rewrite rule in my htaccess file:
RewriteRule ^([^/]+)\.html$ index.php?p=$1 [L]
As soon as that line is in my htaccess file, the redirect variable breaks, and directs the user to a css file, or image. The rewrite rule works as it should, and it is there in order to use nicely formatted urls throughout the website.
I've been trying to troubleshoot this for the last few days but have had no success.
If anyone could shed some light onto this for me, that'd be awesome.
Thanks!
HTACCESS FILE **
I can't explain the issue fully, because I don't understand why it's happening.
Here's my current htaccess file, there are a few different rewrite rules here, but the first one is the one that's causing the problem.
Options -Indexes
Options -MultiViews +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www.yoursafetyexperts.com$
RewriteRule ^(.*)$ http://www.yoursafetyexperts.com/$1 [R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/cms/
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)\.html$ index.php?p=$1 [L] // If I remove this rule, my redirect works properly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/.]+)/([^/.]+)\.php$ index.php?p=$1&post_id=$2&post=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/.]+)/([^/.]+)/$ index.php?p=$1&catId=$2&catName=$3 [L]
</IfModule>

.htaccess to change URL

So i have a mvc system setup but it does not generate search engine friendly urls.
A typical url is in the format:
http://sitedomain.com/class/classMethod?parameter=valueA?parameter2=valueB
This is what i need to have:
http://sitedomain.com/class/valueA/valueB/
My .htaccess actually modified a part of the url already but i dont know how to do the second part
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
(originally looked like http://site.com/index.php?controller=class, but after the htaccess below is ran, it looks like http://site.com/class)
If anyone could help me with this, that would be great, thank you.
RewriteRule ^/class/(.*)/(.*)/$ index.php?controller=class&parameter=$1&parameter2=$2 [L,QSA]
I use the following .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ rewrite.php?%{QUERY_STRING}
And extracting parts from the url is all done in PHP.
Parsing the $_SERVER['SCRIPT_NAME'] variable.
(I find php code much easer to debug than complex apache rewrite rules.)

Categories