Url rewrite [Long url to short] - php

I'm developing CMS and want to make template system. So I could have multiple themes, like Wordpress has. But I have problem with urls.
My question is how to rewrite this url:
http://example.com/themes/mytheme/post.php?slug=some-post-title
to something like this:
http://example.com/post/some-post-title
So point is to cut that part of themes/mytheme

Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} slug=(.*)
RewriteRule ^([^/]*)/([^/]*)/post.php post/%1? [NC]
Tested here.
The RewriteCond detects the query string and allows for a back-substitution with %1 in the RewriteRule.
^([^/]*)/([^/]*)/ matches the first two folders.
The ? at the end of the RewriteRule is an empty query string so that the GET variables won't be passed through.
[NC] means case insensitive comparisons.

Related

Rewrite URL to hide query string

I'm trying to mask the query string of my pages in order to hide it's unique page ID. This is important as each ID needs to be unique to a user.
Currently the URL structure looks like this:
http://domain.com/page.php?Page_ID=1234
(where 1234 is any number)
but I need it to look like this:
http://domain.com/page.php
I have tried adding the following to the .htaccess file but it does not seem to make any difference:
RewriteEngine on
RewriteBase /
RewriteRule ^page.php?Page_ID=([0-9]+)/$ page.php [L,QSA,NC]
I've looked at other posts like this one and others, but can't seem to find a solution. Is there something I might be missing here?
Query string is not part of match in rewrite rule, you need to match against %{THE_REQUEST} using a rewriteCond
RewriteEngine on
RewriteCond %{THE_REQUEST} /page\.php\?page_ID=.+ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]

How to orgnize 301 redirects only for pages with get parmeters?

Here is a quick SEO question. We decided to split our web site into two different sites. Google has already crawled 50K pages which we want to move to another domain name. My question is what would be the best way to deal with it as we want only certain URLs to be redirected not the whole website. Should I do mode rewire catch the get parameters and send them over to the new domain name? or should I do it with php headers?
olddomain.com becomes oldomain.com and newdomain.com
oldomain.com?name=jw&gsurname=black --> newdomain.com?name=jw&gsurname=black
oldomain.com with any other url structure should stay the same
You should be able to use the RewriteCond and RewriteRule directives together to match against query string values, like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^name=([^&]+)&gsurname=([^&]+)$
RewriteRule ^(.*)$ http://newdomain.com?name=%1&gsurname=%2 [R=301,L]
If you mean exactly that set of query parameters:
RewriteCond %{QUERY_STRING} ^name=([^&]+)&gsurname=(.*)$
RewriteRule ^/$ http://newdomain.dom [R=301, L]
The RewriteCond looks at the query string and checks that the first parameter is name and the second parameter is gsurname. The ([^&]+) collects all of the characters until it finds an ampersand (^& means not ampersand). The (.*)$ collects characters until the end of the query string ($).
If the RewriteCond is true, then the RewriteRule redirects to the new domain. The query string is automatically passed along as-is.

CMS Pages in Database .htaccess configuration

In my CMS I store all pages in a database. If you request a page currently it works like this:
Requested URI: www.xyz.com/site.html
.htaccess: mod_rewrite creates: /index.php?q=site
index.php: Looks up in Database for the entry site.
To clean up the URLs I like to have URLS like this: www.xyt.com/about/site.html or www.xyt.com/about/groups/groups.html
In my Database is an entry for every Page called owner which represents the Parent Site.
The Problem for me is that the number of 'folders' is not fixed.
So i thought I should Change www.xyt.com/about/site.php to /index.php?q=about-site in the .htaccess and than write a PHP function which finds the site site with the Parent about
What would be the RewriteRule?
I that a good way or is there an other (better) way?
Changing the foo/bar/about/site/etc.html to index.php?q=foo-bar-about-site-etc is much more difficult with mod_rewrite than it is with PHP. Just do this in php, get the $_GET['q'] variable and explode the string into an array or something using the / flags. It's also better this way because you'll know for sure that the / characters are reserved and you won't end up having to resolve stuff like /foo-bar/about/site. The rules would look something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php\?q=([^&\ ]+)
RewriteRule ^ /%1.html [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /index.php?q=$1 [L]

Rewrite automatically removes backslash if there's more than one?

I have a very simple url rewriting rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !script.php
RewriteRule ^test/(.*) script.php?q=$1
The idea is to have this kind of urls: http://mywebsite.com/test/http://example.com
and then send http://example.com to the script.php as a query parameter. The problem is that I'm receiving http:/example.com instead of http://example.com. Also, http:////example.com would be sent as http:/example.com. What causes this behavior ?
Apache mod_rewrite engine converts multiple ///... into single / for pattern matching in RewriteRule directive. However if you match it using RewriteCond then you can match multiple /s.
You can use rule like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/+test/+(https?://.+)$ [NC]
RewriteRule ^ script.php?q=%1 [L,QSA]
The browser causes this behaviour. It contracts a sequence of / into 1 /, because it is still essentially a path. ///// does not change the directory we are in, so we could as well use /.
You have two options:
Change your links to use a query string instead. If you rewrite test/?q=something to script.php?q=something everything works as expected. You would do the following:
RewriteRule ^test/?$ script.php [L]
Since you don't alter the query string, the original query string is automatically copied to the new query string.
Don't make an assumption on how many slashes you will encounter. The url might not look correctly in the url bar of the browser, but if it is just a redirect, it will only be visible for a very short period of time.
RewriteRule ^test/(http|https):/+(.*)$ script.php?q=$1://$2

Want .htaccess to display / insteade of ? in URL and also i am able to get data like $_GET["param1"]

My Actual URL is below.
http://localhost/waterpump/index.php?param1=4498&param2=930&param3=876&param4=201&param5=vis
But my client want in below format.
http://localhost/waterpump/param1/4498/param2/930/param3/876/param4/201/param5/vis
And also i am able to get data using $_GET["param1"]
How can I do this through .htaccess?
Try examples from htaccess tricks
e.g.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^blog/([0-9]+)-([a-z]+) http://corz.org/blog/index.php?archive=$1-$2 [NC]
I am not sure if the number of parameters if fixed or variable but you would do essentially a matching part, where you match the url separated by slashes and then you would "rewrite" it to the original url
If you are set on using $_GET to get the parameters and there will always be between one and five parameters then you could use the following rewrite:
RewriteEngine On
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/param5/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4&param5=$5 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/ /index.php?param1=$1&param2=$2&param3=$3
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/ /index.php?param1=$1&param2=$2 [nc]
RewriteRule ^waterpump/param1/(.*)/ /index.php?param1=$1 [nc]
Editted
If you have implemented this correctly doing a print_r($_GET); on /waterpump/param1/x/param2/y/param3/z/ Should give you something like:
array (
'param1'=>x,
'param2'=>y,
'param3'=>z,
);
There are cleaner ways of doing this that would involve less rewrites, but some changes to your PHP. Also the method above will only work for waterpumps. Will there be anything other than water pumps?

Categories