Im trying to combine three various clean url rules - .htaccess

my problem is that I want to get three simple rules working, but my knowledge is too little, to get them working together:
These are obvious:
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
domain.com/login and domain.com/register
Secondly, since i have only one page used for displaying data, i want its url to be as simple as posible, like:
RewriteRule ^(.*)$ /data.php?id=$1 [L]
which should be translated into:
domain.com/1a2s3d
As third, I want to be able to change url with activation code:
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
which finally should be translated into:
domain.com/register/activate/some-hash
I know just simply basics. And I cannot mix all of these three ideas into one single working htaccess file. With the second rule the server gives me 500 error, with third rule registration page works, but css file path is translated into domain.com/register/activate/theme/style.css instead of domain.com/theme/style.css
Any help would be appreciated.

Try just with that:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^login$ /login.php [L]
RewriteRule ^register$ /register.php [L]
RewriteRule ^register/activate/([^/]+)$ /register.php?action=activate&code=$1 [L]
RewriteRule ^(.*)$ /data.php?id=$1 [L]

Related

Two parameters for URL Rewrite in HTACCESS

I'm working on a simple five page website which includes a blog. This is the first time I'm using .htaccess to correctly rewrite my URLs for SEO friendly optimization.
The two parameters I am trying to allow a visitor to use is sitename.com/blog/category/WORD or sitename.com/blog/SLUG. If /category/word is used, I'd want it to list all the posts with the chosen category word. If /slug is used (short title of a post such as "learn-how-to-code"), I'd want it to just show that specific post.
Here's my old .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It was originally working fine for single pages, such as sitename.com/about, sitename.com/contact and so on, but any time I tried sitename.com/blog/SLUG or sitename.com/blog/category/WORD it would just redirect to the blog page. I am echoing $_GET['category'] and $_GET['slug'] in blog.php to test it, but it never showed anything.
After looking at a few posts here, I figured out that I needed to add some separate lines to just check on /blog being used and to pull in the appropriate parameters.
Here's my current .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([^\.]+)$ blog.php?slug=$1 [NC,L]
RewriteRule ^blog/category/([^\.]+)$ blog.php?category=$1 [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The sitename.com/blog/SLUG is working fine, but anytime I do sitename.com/blog/category/WORD, it's showing "category/WORD" as the slug, and not breaking it into the category word.
Am I doing something wrong here? Is there a better way of doing this?
Simple explanation without tech is for better clarification
Line1 RewriteRule ^blog/([^\.]+)$ blog.php?slug=$1 [NC,L]
Line2 RewriteRule ^blog/category/([^\.]+)$ blog.php?category=$1 [NC,L]
in line 1 you are doing regular expression checking by code ([^\.]+) after the url blog
and in line 2 your url is kind of hard coded part of category and then you are doing regular expression check
so in simple work regular engine never goes to the line2 of code because it already become part of line1 is already checked and passed regular expression part so category becomes part
simple tip: Always keep hard url parts first and regular expression parts rules at the end.
You'd need to swap your ^blog lines. Since the first one is using the L flag, you're essentially telling it to stop using other rules, so it's already picking up $1 as category/WORD in your case.
I don't know how the rest of your backend is setup, but you could also look into routing everything through an index page.

ReWrite rule not working with two parametrs

im quite new to rewrite rules.
I can manage with one variable and thats it.
I have webpage Where the rewriterule is:
RewriteCond %{HTTP_HOST} ^www\.someserver\.com$
RewriteRule ^/?$ "http\:\/\/someserver\.com\/" [R=301,L]
RewriteRule ^(\d+)*$ ./index.php?comp=$1
RewriteRule ^(\d+)*/$ ./index.php?comp=$1
And it all work fine as it should. But now as i want 1 more variable into URL
i cant get it to work.
Right now the $1 is only numbers.
someserver.com/1554886
But i want two variable.
someserver.com/1554886-SOMENAME-WHATEVER-WORD-AND-HOW-MANY
But it wont show.
i tried smth like this:
RewriteRule ^([^-]*)-([^-]*)$ ./index.php?comp=$1&string=$2 [L]
How do i get it to work?
Do i have to make some changes in the php side as well?
everything what comes after the number part of the URL is there only for
SEO, the number is Unique
You need one more rule to handle two parameters:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(someserver\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(\d+)/?$ index.php?comp=$1 [L,QSA]
RewriteRule ^(\d+)-(.+)/?$ index.php?comp=$1&string=$2 [L,QSA]

.htaccess - clean urls with different parameters

Is there a way to get this to work?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=1&b=2 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=$1&c=$2 [L]
The urls should look like this:
http://test.com/a/b
or
http://test.com/a/c
It only works with the first rule.
In my case I'm trying to create a profile page and get the ID either of the session ID or via $_GET['id'].
So if I visit a profile of someone else the url is
index.php?page=profile&id=25
/profile/25
And if I'm visiting my own profile it is
index.php?page=profile
/profile
And for example I want to edit my profile it is
index.php?page=profile&action=edit
/profile/edit
I hope you understand what I mean and can help me.
The key to solving this is noticing the differences between each parameter you want to pass.
If the visitor is looking at someone else's profile, an id (numeric) is passed.
If the visitor is editing their profile, a parameter string (alphanumeric) is passed
If the visitor is looking at their own profile, or another generic page, no extra parameters are passed
.htaccess rules can be most easily written from the most specific to the most general, so translating this, the rules become
RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L]
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
It is also important to skip existing files & directories, however because RewriteCond statements only match the next RewriteRule, it is easiest to do this in a slightly different way than you are doing it.
RewriteEngine On
# If the file or directory exists, exit
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [END]
RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L]
RewriteRule ^([^/]+)$ index.php?page=$1 [L]

htaccess two parameter mod-rewrite problems

I'm trying to setup a more friendly URL system and failing miserably. I want to be able to pass 1 or 2 GET parameters like this:
http://website.com/1234/123456
where 1234 is the first param and 123456 is the second param.
In my attempts Apache keeps viewing the /1234/ as a folder and a parameter. Here's what I've tried so far:
RewriteRule ^([^/]*)/([^/]*)$ index.php?id=$1&pa=$2 [L]
and
RewriteRule ^(.*)/(.*)$ index.php?id=$1&pa=$2
RewriteRule ^(.*)$ index.php?id=$1 [L]
You could do it like this:
RewriteEngine On
RewriteRule ^/?index\.php$ - [L]
RewriteRule ^/?(?:([^/]*)(?:/([^/]*)/?)?)?$ /index.php?id=$1&pa=$2 [L]
It's a little strict and messy, so you could just try:
RewriteEngine On
RewriteRule ^/?index\.php$ - [L]
RewriteRule ^/?(([^/]*)(/?(.*)))?$ /index.php?id=$2&pa=$4 [L]
That will take everything past the last so host.com/123/1233/56 will rewrite to host.com/index.php?id=123&pa=1233/56 while the first won't rewrite it, because of the 56

Trying to add trailing slash with htaccess, results in a absolute path

What I'm trying to achive is to have all urls on my page look like http://domain.com/page/, no extensions, but a trailing slash. If a user happends to write http://domain.com/page or http://domain.com/page.php it will redirect to the first url. After some googling i found this code, and it's close to working, but when you leave out the trailing slash in your request the url becomes something like http://domain.com/Users/"..."/page/ and therefor returns a 404.
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !(.*)/$
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
I've been trying to add an additional rule but I really don't get any of this and I haven't been able to find any answers.
For a scenario like this one, the .htaccess author has to consider both what the browser URL bar should display and what file the web server should return/execute. Note also that each external redirect starts the processing of the rewrite directives over.
With that in mind, start by taking care of which file is returned when the URL is in the correct format:
RewriteEngine on
RewriteRule ^/?$ /index.php [L]
RewriteRule ([^./]+)/$ /$1.php [L]
Then, deal with URLs with no trailing slash by redirecting them with [R=301]:
RewriteRule ^/(.*)\.[^.]*$ http://www.example.com/$1/ [R=301,L]
RewriteRule ^/(.*)$ http://www.example.com/$1/ [R=301,L]
Note that the first of these two rules should also take care of the case where there is a filename (like something.php) but also a trailing slash by eliminating the filename extension and re-adding the slash.
Keep in mind that, if your internal directory structure does not match what the web server is serving (as is often the case in shared hosting scenarios), you will likely need to add a RewriteBase directive immediately after the RewriteEngine directive. See the Apache docs for an explanation.

Resources