htaccess rewrite url query string with index as base - .htaccess

New poster to this place but long term user.
I have a site which uses index.php and then has a viewpost.php and categorypost.php.
The viewpost is for viewing the main posts and the categorypost is for viewing posts under a category such as "webdesign".
The view post slug is made up of query string such as:
viewpost.php?postType=services&postCategory=webdesign&postTitle=somepost
I then want that to rewrite to
website/services/webdesign/somepost
First question:
Is this the correct way to do this by using 3 files or am I creating more work than needed?
Second question:
If I'm on the correct or at least an ok path, how do I go about redirecting?
I have seen about 30 or so posts around this but finding it hard to get my head around it and have ended up with 500 response or just straight redirecting.
RewriteRule ^(.*)$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]
Any help is greatly appreciated.

If these particular links can all start with the same word, it is the simplest, in this case "website". Because it avoids mistakenly using an existing folder with a missing page.
RewriteRule ^website/([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [NC,QSA,L]
But if it's not the case, you can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]

Related

Tricky .htaccess mod_rewrite syntax problem

I got stuck, and even reading through tons of forum posts didn't help me.
The challenge:
I need URIs to be rewritten and queries to be maintained
Examples 1:
example.com/test/23/result/7
shall be redirected to a script under
example.com/test/
That works quite well with an .htaccess entry like this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^test/(.+)$ test/?s=$1
The URI is displayed unaltered. The called script is called, and the additional subdirectory definitions can be retrieved in PHP either through variable $_GET['s'] or $_SERVER['REQUEST_URI']. All is fine so far. The problem starts when adding a query string:
Example 2:
example.com/test/23/result/7?id=16
shall be redirected to the same script under
example.com/test/?id=16
Even when I add [QSA] to the rewrite rule, the URI is not parsed correctly. I tried several ways to initiate a redirect. All failed. The redirect either points to a non-existing address or the query string gets lost. Besides the initial URI subdirectory information, here I would need the query string to be evaluated in my script. Both pieces of data need to be transferred to it.
Does anyone have a solution?
Thanks a lot for sharing your expertise!
I would go with following htaccess Rules. This assumes that you have index.php file which is taking care of non-existing pages request in later your Rules.
RewriteEngine ON
##Rules for handling index.php url here.
RewriteCond %{THE_REQUEST} \s/([^/]*)/.*\?index\.php\s [NC]
RewriteRule ^ %1?%{QUERY_STRING} [NC,L]
##Rules for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
###Rest of your rules go here.....

What is the proper way to make your url "nicer"?

recently i have tried to build some MVC application without any framework to understand MVC pattern better. Till now i have resolved every problem i have had BUT....
Pretty common thing is to make your URL looks "nicer"
For example www.somesite.com/controller/method
instead of www.somesite.com/index.php?c=1&m=2.
i achieved this simply with htacces by aiming it to a variable.
in htaccess...RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This works perfectly fine until someone tries to rewrite variable "url".
Basically if someone types www.somesite.com/controller/method?url=1
my Application will pop up an error page because i am parsing everything after / and calling specific controllers and methods by its name (or popping up an error page if that doesnt exists).
So i would like to know if there is a better way to do this or way to avoid this behaviourThanks :)
EDIT
In last few hours i tried to find a better solutions. I thought i could put my url into Enviromental variable instead of into get variable.
So i experimented with commands like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [E=URL:$1,QSA]
unfortunately $_SERVER["URL"] is blank......
i would be really happy if someone could help me with this piece of code :) Thanks
EDIT 2
Okay to make it clear i'll add few examples.
My current htacces looks like this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA]
So if someone goes to www.somesite.com/foo/bar
i will check $_GET["url"] and then explode it explode("/", filter_var(rtrim($_GET["url"], "/"), FILTER_SANITIZE_URL))
Now i have array which looks like [0=>foo 1=>bar]
That means in my code that i will try to call controller called foo and then method in this class called bar
In case that the user will try to acces www.somesite.com/foo/bar/fee/faa, fee and faa will pass as a parameters to method bar in class foo.
So this was just example how does this work. My problem is as i said when someone tries to acces lets say www.somesite.com/foo?url=0. Then my script will try to handle $_GET["url"] and the result wont be foo but 0 because of ?url=0 rewrites the value of url which was originaly set in my .htacces. So my scripts will try to call controller called 0 and if that doesnt exists itt will popup error404. I have already tried to ignore this specific variable via QUERY_STRING in htacces but this seems to me like a stupid solution. For now i would like to stick with setting evniromental variable instead of get variable or if there is some better way to achieve this :) Thanks
Your current set-up relies on $_GET to obtain core information but, as you've faced, that variable is populated from user input so anyone can mess with your routing, even inadvertently.
A typical Apache configuration for a custom router looks like this (this snippet is from CakePHP/2.x):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
As you can see is doesn't add GET variables of its own, it merely lets existing ones to pass through. Original URL is right there inside $_SERVER, you don't need to instruct Apache to copy it into the redirected URL.
I understand you want $_SERVER['REQUEST_URI'] but you can peek inside the array with the usual methods: print_r($_SERVER), var_dump($_SERVER), phpinfo()...

Multiple Clean Url Variables

I was wondering how to pass several (two) values through url as a clean url.
I've done clean urls before, but never with multiple values and it doesn't seem to be working.
This is what the url looks like now:
http://example.com/?user=Username&page=1
This is what I want it to look like
http://example.com/user/Username/page/1
I've tried other answers that I've seen on here, but they aren't working for this certain deal.
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# Match the first two groups before / and send them to the query string
RewriteRule ^([^/]+)/([^/]+) index.php?user=$1&page=$2 [L]
Thanks. :)
I'm using PHP by the way. :)
Also, will I still be able to use $_GET with this? I thought so, but I also somewhere else where it said you can't... :D
You're missing several matches, try:
RewriteEngine On
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?$1=$2&$3=$4 [L]
This will take a URL like:
http://example.com/a/b/c/d
to the URI:
/index.php?a=b&c=d
will I still be able to use $_GET with this? I thought so, but I also somewhere else where it said you can't.
In the above example, when you look at $_GET['a'] you'd get b.

changing link url using .htacces

This is the link http://djmobi.in/?dir=/Mobile_Ringtones&p=1&sort=1/Mobile_Ringtones.html
Please tell me how to remove ?dir= from the above link I want to make it look like below link eg.
http://www.finewap.com/Category/9497/Mobile_Ringtones.html
You must not think the way "How to remove ?dir=", but "Which link use instead of this complex one".
This is the kinf of .htaccess you'll need :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)\.html$ index.php?dir=$1 [L]
This .htaccess will redirect each url like http://www.domain.com/foo.html to http://www.domain.com/index.php?dir=foo (this will be transparent)
This is a good start, I let you search for some documentation to add your pages and sort management (we don't have enought datas to give you a working piece of code)

How to avoid URL and directory match problem when editing .htaccess file?

I have the following lines in the .htaccess file in the site directory:
RewriteEngine On
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
If it receives URL for example :
http://localhost/samplesite/johnsmith
it will rewrite it to
http://localhost/samplesite/account.php?username=johnsmith
which is fine.
The problem occurs when there is a directory named johnsmith in the site directory. then the URL is rewritten to and displayed as
http://localhost/samplesite/johnsmith/?username=johnsmith
and that is a problem. I am trying to implement account pages functionality for every user but if a user wants to register a username like some directory in the root the functionality will break? I tried adding rewrite conditions to check if the requested URL stands for an existing directory or a file :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
but I don't know how to proceed.
If someone knows a better way to do account pages functionality for users I would appreciate to give me a piece of advice on that.
Can anybody help me solve this case? Thank you!
Your RewriteCond will work if it is in the correct place. Your full .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# ---------------------------------------------------------------See info below--
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
Also, you don't need the %{QUERY_STRING}. Instead you should use the QSA flag to append the rest of the query string...
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1 [L,QSA]

Resources