33825

URL rewrite(SLUG) and URL redirection (htaccess)

Question:

Below is my php code(qs.php). This file contain url links. Pretty links created by using SLUG.

<html> <head></head> <body> <?php function create_slug($string){ $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string); $slug = strtolower($slug); return $slug; } $slugurl= create_slug('SLUG-text-testing-purpose-only'); $file = 'qs1'; //this is a php file qs1.php $id = '12345678'; //testing ID $complete_url = $file."/".$id."/".$slugurl; ?> <a href ="<?php echo $full;?>"> This is a test link created by slug</a> </body></html>

Above link appear like this - http://localhost/qs/qs1/12345678/slug-text-testing-purpose-only

<strong>Edit</strong>

QS is a directory where all files are placed. QS1 is a php file (qs1.php)

I want to get two variable from above link 12345678 & slug-text-testing-purpose-only. as qs1.php is file where i get these two variables from $_GET['var1'] & $_GET['var2'];

I know it can be achieved by .htaccess. i tried multiple options but they are not working as i am getting 404.

.htaccess file is placed on QS directory.

Below are the options i had tried.

RewriteEngine On RewriteRule ^qs/([0-9]+)/([^/]+)$ qs1.php?var1=$1&var2=$2 [L] RewriteRule ^/([^/]*)/([^/]*)$ /qs1.php?var1=$1&var2=$2 [L] RewriteRule ^qs/([^/]+)/([^/]+)/?$ /qs1.php?var1=$1&var2=$2 [QSA,L] RewriteRule /qs/(\d+)/(.*) qs1.php?id=$1&slug=$2 [QSA,L]

I am very new to htaccess and url redirect.

Please advise.

Thanks

Answer1:

You can put this code in /qs/.htaccess

RewriteEngine On RewriteBase /qs/ RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [NC,L]

Now, you can go to http://localhost/qs/qs1/12345678/slug-text-testing-purpose-only and you'll get the same content as /qs/qs1.php?var1=12345678&var2=slug-text-testing-purpose-only

Answer2:

RewriteEngine On RewriteRule ^qs/qs1/(\d+)/(.+)$ qs1.php?var1=$1&var2=$2 [NC,L]

You can play with it here: <a href="http://htaccess.madewithlove.be" rel="nofollow">http://htaccess.madewithlove.be</a>

Recommend

  • Undefined Offet Error in cURL Code
  • Converting uppercase html tags to lowercase
  • Replacing array key spaces with underscores in multidimensional array
  • image captcha in php
  • NoReverseMatch at / Reverse error with urlresolvers, get_absolute_url()
  • Why won't this override of the Model.save() function in Django work?
  • How to remove special characters in file names?
  • PHP Explode from first number (integer)
  • Mongodb $addToSet of nested object
  • RegEx for Phone Number Numbers with Letters
  • Parsing tokens with PLY
  • php regex remove digits
  • How to get a table cell value using jQuery
  • Obtain actual browser URL in PHP
  • How to add regEx in angular filter
  • Submit a form with jQuery / Javascript without ignoring “required” tag
  • Replace and retrieve placeholder value
  • Adding directive inside the directive programatically
  • How to map Request parameter in Spring?
  • Responsive Form on top of Responsive Image? - Bootstrap
  • Regex for URL rewrite with optional query string parameters
  • Form tag not showing up in haml file
  • Regex to match a string not followed by anything
  • Angularjs pass function from Controller to Directive (or call controller function from directive) -
  • Simulate click Geckofx vb,net
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Jquery UI tool tip close icon
  • ImageMagick, replace semi-transparent white with opaque white
  • Email format validation in mvc3 view
  • HTML download movie download link
  • MySQL WHERE-condition in procedure ignored
  • sending/ receiving email in Java
  • Web-crawler for facebook in python
  • trying to dynamically update Highchart column chart but series undefined
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?